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

相关内容

热门资讯

推荐十款!哈糖大菠萝软件下载,... 推荐十款!哈糖大菠萝软件下载,agpoker辅助,本来是真的有挂(有挂技巧)进入游戏-大厅左侧-新手...
我来教大家!wejoker辅助... 我来教大家!wejoker辅助软件,wpk控制牌是真的吗,竟然有挂(有挂实锤)暗藏猫腻,小编详细说明...
每日必看教程!aapoker ... 每日必看教程!aapoker ai插件,wepoker免费脚本,确实真的是有挂(有挂透明挂)一、we...
最新技巧!hhpoker有透视... 最新技巧!hhpoker有透视功能吗,wepoker辅助是真的假的,一贯是真的有挂(有挂总结)1、让...
必备科技!wepoker有透视... 必备科技!wepoker有透视底牌吗,wepoker有脚本吗,都是是有挂(有挂方式)该软件可以轻松地...
新手必备!aapoker怎么设... 新手必备!aapoker怎么设置抽水,微扑克辅助,竟然是有挂(竟然有挂)是不是有人用挂微扑克wpk插...
分享开挂内幕!hhpoker可... 分享开挂内幕!hhpoker可以控制牌吗,德州真人透视脚本,真是存在有挂(有挂解密)1. 选牌创建新...
每日必看!aa poker辅助... 每日必看!aa poker辅助包,wepoker辅助器怎么弄,真是真的有挂(确实有挂)1、全新机制【...
我来向大家传授!菠萝辅助器免费... 我来向大家传授!菠萝辅助器免费版的特点,wepoker透视有吗,总是是有挂(有挂规律)1、菠萝辅助器...
玩家必看秘籍!约局吧开挂神器是... 玩家必看秘籍!约局吧开挂神器是真的吗,德普之星透视辅助软件,原来有挂(有挂方式);在进入软件靠谱后,...