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辅助(a... 黑科技最新!来玩app辅助(aapoker真的有猫腻吗)原来有挂(有挂透视)-哔哩哔哩1、很好的工具...
黑科技数据!xpoker透视辅... 黑科技数据!xpoker透视辅助(aapoker辅助工具ai)总是是真的有挂(有挂规律)-哔哩哔哩是...
黑科技辅助挂!德扑之星软件辅牌... 黑科技辅助挂!德扑之星软件辅牌器(aapokerai辅助)原来真的有挂(证实有挂)-哔哩哔哩1、这是...
黑科技辅助!云扑克软件(德扑a... 1、黑科技辅助!云扑克软件(德扑ai智能机器人线上代打)起初真的是有挂(有挂攻略)-哔哩哔哩;该软件...
黑科技插件!智星德州安卓版下载... 黑科技插件!智星德州安卓版下载方法(wepokeai代打)最初是有挂(真实有挂)-哔哩哔哩1、wep...
黑科技挂!wepokeai代打... 黑科技挂!wepokeai代打辅助(德扑ai机器人软件开发)果然有挂(有挂教学)-哔哩哔哩;德扑ai...
黑科技ai代打!聚星扑克有假(... 黑科技ai代打!聚星扑克有假(aa poker有外挂吗)果然真的是有挂(有挂规律)-哔哩哔哩,您好,...
黑科技苹果版!德州ai机器人(... 黑科技苹果版!德州ai机器人(wpk最新黑科技)从前存在有挂(有挂规律)-哔哩哔哩1、许多玩家不知道...
黑科技讲解!德州之星有辅助挂(... 您好,德扑ai助手软件这款游戏可以开挂的,确实是有挂的,需要了解加威信【136704302】很多玩家...
黑科技插件!微扑克如何让系统发... 黑科技插件!微扑克如何让系统发好牌(aa poker有外挂吗)果然有挂(有挂详情)-哔哩哔哩1、微扑...