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文件中添加了权限声明,以获取振动权限。

相关内容

热门资讯

透视真的(wPk)ai辅助有没... 透视真的(wPk)ai辅助有没有用(黑科技)ai机器人(本来有挂)1、超多福利:超高返利,海量正版游...
了解辅助(微扑克)辅助提高胜率... 自定义系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用辅助器,不管你是想分享给你好...
透视安卓版(Wpk)透视辅助测... 自定义系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用辅助器,不管你是想分享给你好...
大神辅助(微扑克)辅助工具(辅... 大神辅助(微扑克)辅助工具(辅助挂)如何让系统发好牌(一直存在有挂)1、ai辅助优化,发牌逻辑科技护...
黑科技透明(wepoker)软... 黑科技透明(wepoker)软件透明演示(黑科技)ai代打(真是真的有挂)1、构建自己的微扑克辅助插...
详细辅助(微扑克)辅助测试(辅... 详细辅助(微扑克)辅助测试(辅助挂)辅助提高胜率(都是有挂)1、不需要AI权限,帮助你快速的进行计算...
黑科技攻略(wepoKE)ai... 黑科技攻略(wepoKE)ai软件(黑科技)辅助使用教程(一贯是真的有挂)1、超多福利:超高返利,海...
大神辅助(微扑克)ai辅助(辅... 大神辅助(微扑克)ai辅助(辅助挂)如何让系统发好牌(一直真的是有挂);是一款可以让一直输的玩家,快...
黑科技存在(wepoKE)ai... 一、简介了解软件请加微:136704302是一款在线扑克游戏平台,玩家可以在平台上进行多人在线扑克游...
攻略辅助(微扑克)辅助测试(辅... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多玩家在这款游戏中打牌都...