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

相关内容

热门资讯

黑科技攻略!pokermast... 黑科技攻略!pokermaster有德州吗(智能ai)原本有挂(有挂自建房)-哔哩哔哩是一款可以让一...
黑科技挂!aa扑克德州辅助工具... 黑科技挂!aa扑克德州辅助工具,we辅助poker德之星,普及教程(有挂教学)-哔哩哔哩1)aa扑克...
黑科技安装!fish poke... 黑科技安装!fish poker外挂(辅助挂)原先存在有挂(有挂规律)-哔哩哔哩;原来确实真的有挂(...
黑科技透视!wpk有透视辅助吗... 黑科技透视!wpk有透视辅助吗,aapoker外挂实测,新2025教程(有挂技巧)-哔哩哔哩(1)黑...
黑科技规律!微扑克有辅助挂吗(... 黑科技规律!微扑克有辅助挂吗(辅助挂)果然是有挂(有挂免费)-哔哩哔哩;建议优先通过微扑克有辅助挂吗...
黑科技辅助挂!aapoker辅... 1、黑科技辅助挂!aapoker辅助工具存在吗,德州ai辅助,新2025版(有挂教学)-哔哩哔哩;代...
黑科技好友!德州之星透视辅助器... 黑科技好友!德州之星透视辅助器(ai代打)起初是有挂(有挂模拟器)-哔哩哔哩;1.德州之星透视辅助器...
黑科技神器!we-poker有... 黑科技神器!we-poker有挂吗,wepoke透明挂咋测试,黑科技教程(有挂总结)-哔哩哔哩,您好...
黑科技挂!wpk微扑克智能辅助... 黑科技挂!wpk微扑克智能辅助(黑科技)都是存在有挂(有挂规律)-哔哩哔哩;玩家必备必赢加哟《136...
黑科技机器人!wpk ai机器... 黑科技机器人!wpk ai机器人,智星德州菠萝怎么看有没有开挂,切实教程(有挂方法)-哔哩哔哩;无聊...