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

相关内容

热门资讯

开挂透视!hhpoker是正品... 开挂透视!hhpoker是正品吗,微乐河南家乡麻将辅助脚本,技法教程(有挂攻略)-哔哩哔哩1、玩家可...
专业透视!哈糖大菠萝可以开挂吗... 专业透视!哈糖大菠萝可以开挂吗,微信微乐辅助器下载,积累教程(有挂总结)-哔哩哔哩1、哈糖大菠萝可以...
详情透视!wejoker辅助器... 详情透视!wejoker辅助器要钱玩吗,微信小程序微乐辅助器,机巧教程(真的有挂)-哔哩哔哩暗藏猫腻...
详细透视!wepoker有透视... 您好,wepoker有透视底牌吗这款游戏可以开挂的,确实是有挂的,需要了解加去威信【48527505...
科普透视!aapoker怎么选... 科普透视!aapoker怎么选牌,微乐斗地主有挂吗,指南书教程(有挂存在)-哔哩哔哩1、进入游戏-大...
专业透视!pokerrrr2辅... 专业透视!pokerrrr2辅助,微信卡五星小程序辅助,诀窍教程(了解有挂)-哔哩哔哩1.poker...
关于透视!德普之星透视免费,微... 关于透视!德普之星透视免费,微乐游戏小程序脚本,妙招教程(有挂解惑)-哔哩哔哩1、进入到德普之星透视...
了解透视!德普之星透视辅助,微... 了解透视!德普之星透视辅助,微乐自建房辅助工具在哪,课程教程(有挂规律)-哔哩哔哩1、完成德普之星透...
揭露透视!德普之星透视辅助软件... 揭露透视!德普之星透视辅助软件激活码,微乐小程序辅助工具2025,攻略教程(竟然有挂)-哔哩哔哩一、...
分享透视!hhpoker智能辅... 分享透视!hhpoker智能辅助插件,手机微信小程序游戏破解器下载,绝活儿教程(果真有挂)-哔哩哔哩...