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

相关内容

热门资讯

最新技巧!长城麻将外卦神器软件... 最新技巧!长城麻将外卦神器软件(透视挂),长城麻将好像存在有透视挂项目1)长城麻将免费钻石:进一步探...
分享给玩家!未来云南麻将开挂脚... 分享给玩家!未来云南麻将开挂脚本(透视挂),未来云南麻将竟然是有透视挂模块1、在未来云南麻将插件功能...
玩家必看秘籍!颖河竞技怎么开挂... 玩家必看秘籍!颖河竞技怎么开挂(外挂),颖河竞技确实真的是有外挂脚本1)颖河竞技有没有挂:进一步探索...
总算了解!新毫互娱透视挂软件(... 总算了解!新毫互娱透视挂软件(开挂),新毫互娱真是有开挂妙计该软件可以轻松地帮助玩家将新毫互娱外卦神...
科技揭秘!四川麻将有没有透视挂... 科技揭秘!四川麻将有没有透视挂(透视挂),四川麻将切实真的是有透视挂步骤1、下载好四川麻将透视辅助下...
玩家必看分享!南召托牌真的有挂... 玩家必看分享!南召托牌真的有挂(外挂),南召托牌总是是真的有外挂指南书1、点击下载安装,南召托牌脚本...
交流学习经验!全来跑分麻将可以... 交流学习经验!全来跑分麻将可以开挂吗(作弊器),全来跑分麻将原来是真的有作弊器手册1、全来跑分麻将脚...
热门推荐!元来黄冈麻将真的确实... 热门推荐!元来黄冈麻将真的确实有挂(外挂),元来黄冈麻将本来真的有外挂脚本1、用户打开应用后不用登录...
玩家爆料!悠闲麻将川南是如何开... 玩家爆料!悠闲麻将川南是如何开挂的(外挂),悠闲麻将川南总是是真的有外挂模板1、起透看视 悠闲麻将川...
实测发现!微友南昌麻将有没有挂... 实测发现!微友南昌麻将有没有挂吗(透视挂),微友南昌麻将都是是有透视挂演示1、微友南昌麻将透视辅助软...