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有辅助挂)... 黑科技中牌率(wpk有辅助挂)外挂黑科技辅助软件(透视)确实真的是有挂(黑科技黑科技);所有人都在同...
黑科技模拟器(wepoke辅助... 黑科技模拟器(wepoke辅助机器人)外挂透明挂辅助方法(透视)本来是有挂(黑科技攻略);1、完成w...
黑科技能赢(德扑之星辅助器)外... 黑科技能赢(德扑之星辅助器)外挂透视辅助软件(透视)真是是真的有挂(黑科技解密)1、黑科技能赢(德扑...
黑科技私人局(德州ai辅助ap... 黑科技私人局(德州ai辅助app)外挂透明挂辅助app(透视)果然是有挂(黑科技攻略)1.德州ai辅...
黑科技脚本(wepoke透视该... 黑科技脚本(wepoke透视该购买渠道)外挂黑科技辅助安装(透视)总是存在有挂(黑科技技巧)1、这是...
黑科技辅助(wpk透明挂)外挂... 黑科技辅助(wpk透明挂)外挂黑科技辅助app(透视)本来真的有挂(黑科技教程);在进入wpk透明挂...
黑科技辅助(wepoke辅助器... 黑科技辅助(wepoke辅助器真的有效果吗)外挂黑科技辅助插件(透视)一直真的有挂(黑科技插件);1...
黑科技辅助(wepoke辅助技... 黑科技辅助(wepoke辅助技巧)外挂透视辅助技巧(透视)其实是有挂(黑科技方法)1、每一步都需要思...
黑科技辅助挂(wepokeai... 黑科技辅助挂(wepokeai代打逻辑)外挂透明挂辅助插件(透视)果然真的是有挂(黑科技解说)一、w...
黑科技挂(红龙扑克辅助工具)外... 黑科技挂(红龙扑克辅助工具)外挂黑科技辅助插件(透视)竟然存在有挂(黑科技攻略)1、玩家可以在红龙扑...