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

相关内容

热门资讯

透视指南!pokerworld... 透视指南!pokerworld破解版下载(透视)佛手大菠萝13道挂哪里(辅助)竟然真的是有app(哔...
透视课程!pokemmo脚本辅... 透视课程!pokemmo脚本辅助器下载(透视)德州透视插件(辅助)真是是真的有攻略(哔哩哔哩)进入游...
透视总结!sohoo辅助(透视... 透视总结!sohoo辅助(透视)拱趴大菠萝开挂方法(辅助)一直真的是有攻略(哔哩哔哩)1、全新机制【...
透视大纲!hhpoker万能辅... 透视大纲!hhpoker万能辅助器(WePoKer新版本)总是真的是有辅助软件(哔哩哔哩)1、完成h...
透视阶段!pokemmo脚本(... 透视阶段!pokemmo脚本(透视)来玩app 德州 辅助(辅助)原来是有神器(哔哩哔哩)一、来玩a...
透视指南书!wepoker辅助... 透视指南书!wepoker辅助器激活码(HHpoker控制牌)都是存在有辅助脚本(哔哩哔哩)进入游戏...
透视技法!wepoker透视功... 透视技法!wepoker透视功能下载(HHpoker辅助)其实真的是有辅助教程(哔哩哔哩)1、很好的...
透视举措!wepoker轻量版... 透视举措!wepoker轻量版有透视吗(WePoKer最新)原来是真的有辅助app(哔哩哔哩)1、全...
透视模板!pokemmo脚本(... 透视模板!pokemmo脚本(透视)德州真人透视脚本(辅助)真是是有技巧(哔哩哔哩)亲,关键说明,德...
透视妙计!云扑克有透视吗(透视... 您好,pokemmo脚本辅助下载这款游戏可以开挂的,确实是有挂的,需要了解加去威信【13670430...