Android前台服务播放振动
创始人
2024-10-09 15:02:01
0

以下是一个示例代码,用于在Android前台服务中播放振动:

  1. 创建一个名为"VibrationService"的前台服务类:
public class VibrationService extends Service {

    private static final int NOTIFICATION_ID = 1;

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        startForeground(NOTIFICATION_ID, createNotification());
        startVibration();
        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        stopVibration();
    }

    private void startVibration() {
        Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        if (vibrator != null && vibrator.hasVibrator()) {
            long[] pattern = {0, 1000, 1000}; // 振动间隔:0ms延迟、1000ms振动、1000ms暂停
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                VibrationEffect vibrationEffect = VibrationEffect.createWaveform(pattern, 0);
                vibrator.vibrate(vibrationEffect);
            } else {
                vibrator.vibrate(pattern, 0);
            }
        }
    }

    private void stopVibration() {
        Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        if (vibrator != null) {
            vibrator.cancel();
        }
    }

    private Notification createNotification() {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "channel_id")
                .setSmallIcon(R.drawable.ic_notification)
                .setContentTitle("Foreground Service")
                .setContentText("正在播放振动")
                .setPriority(NotificationCompat.PRIORITY_DEFAULT);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel("channel_id", "Foreground Service", NotificationManager.IMPORTANCE_DEFAULT);
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            if (notificationManager != null) {
                notificationManager.createNotificationChannel(channel);
            }
        }

        return builder.build();
    }
}
  1. 在"AndroidManifest.xml"文件中注册前台服务:


    

    

        
        
        

    


  1. 在需要启动前台服务的地方调用以下代码:
Intent intent = new Intent(context, VibrationService.class);
ContextCompat.startForegroundService(context, intent);

注意:在AndroidManifest.xml文件中添加了权限声明,以获取振动权限。

相关内容

热门资讯

aapoker透明挂!aapo... aapoker透明挂!aapoker辅助软件,(aapoker俱乐部后台)一贯有挂(详细透视辅助教程...
德扑ai助手!德扑之星电脑版,... 《德扑ai助手软件透明挂》是一款多人竞技的德扑ai助手辅助透视游戏,你将微扑克对手来到同一个战场,为...
微扑克全自动机器人!微扑克系统... 微扑克全自动机器人!微扑克系统是不是有问题,(微扑克代打)一直真的是有挂(详细wpk透视辅助教程);...
红龙扑克辅助工具!红龙扑克有没... 红龙扑克辅助工具!红龙扑克有没有作假,(红龙扑克)竟然存在有挂(详细辅助教程);值得一提的是,计算辅...
微扑克辅助器ios!微扑克发牌... 微扑克辅助器ios!微扑克发牌有问题吗,(微扑克内置)确实有挂(详细有辅助挂教程);玩家必备必赢加哟...
aapoker透明挂!aapo... aapoker透明挂!aapoker脚本透明,(aapoker苹果版)切实真的有挂(详细辅助工具存在...
wepoke辅助有挂!wepo... wepoke辅助有挂!wepoke开发者(wepoke游戏)果然是真的有挂(详细辅助技巧教程);we...
德扑之星花钱!德扑之星系统发牌... 德扑之星花钱!德扑之星系统发牌机制,德扑ai软件真是真的有挂(详细房间设置教程);支持多人共享记分板...
德州之星有外卦挂!德扑胜率计算... 德州之星有外卦挂!德扑胜率计算软件,德扑之星开桌真是是有挂(详细智能教程)1、点击下载安装,微扑克w...
微扑克辅助软件!微扑克辅助器是... 微扑克辅助软件!微扑克辅助器是骗人吗,(微扑克)其实是有挂(详细辅助机器人教程)1、每一步都需要思考...