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

相关内容

热门资讯

黑科技辅助挂(德州免费辅助神器... 黑科技辅助挂(德州免费辅助神器app)外挂透视辅助方法(透视)好像存在有挂(黑科技工具)-哔哩哔哩1...
黑科技神器!wepoke辅助挂... 黑科技神器!wepoke辅助挂,智星德州菠萝有挂吗,透牌教程(有挂教程)-哔哩哔哩您好,智星德州菠萝...
黑科技app(WpK)微扑克a... 黑科技app(WpK)微扑克ai辅助神器(透视)辅助器(竟然真的是有挂);原来确实真的有挂(需添加指...
黑科技能赢!微扑克怎么加入俱乐... 黑科技能赢!微扑克怎么加入俱乐部(黑科技ai)切实是真的有挂(有挂后台)-哔哩哔哩;科技安装教程;1...
黑科技模拟器(wpk透视外挂购... 黑科技模拟器(wpk透视外挂购买渠道)外挂黑科技辅助助手(透视)果然真的有挂(黑科技介绍)-哔哩哔哩...
黑科技机器人!德扑之星辅牌软件... 黑科技机器人!德扑之星辅牌软件,微扑克透牌真假的,可靠技巧(有挂总结)-哔哩哔哩是一款可以让一直输的...
黑科技辅助挂(wpK)黑科技(... 黑科技辅助挂(wpK)黑科技(透视)ai辅助购买渠道(从来真的有挂);亲,其实确实真的有挂(需添加指...
黑科技存在!aapoker辅助... 黑科技存在!aapoker辅助透视(透视)切实是真的有挂(有挂输赢)-哔哩哔哩关于aapoker辅助...
黑科技辅助挂(aapoker辅... 黑科技辅助挂(aapoker辅助工具使用教程)外挂透明挂辅助下载(透视)好像真的有挂(黑科技介绍)-...
黑科技挂!wepok软件透明挂... 黑科技挂!wepok软件透明挂,wepoke辅助,可靠教程(有挂方法)-哔哩哔哩是一款可以让一直输的...