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

相关内容

热门资讯

一分钟教会你!哥哥打大a辅助,... 一分钟教会你!哥哥打大a辅助,微信微乐辅助器下载,详细方法(有挂猫腻)1、下载好微信微乐辅助器下载正...
实测揭晓!创思维激k辅助神器,... 实测揭晓!创思维激k辅助神器,琼崖海南麻将辅助器,详细器(有挂讲解)1、完成琼崖海南麻将辅助器有辅助...
发现一款!微乐小程序辅助器代理... 发现一款!微乐小程序辅助器代理,好友赣南脚本插件,详细脚本(确实有挂);微乐小程序辅助器代理能透视中...
玩家必看!边锋老友荣成辅助器,... 玩家必看!边锋老友荣成辅助器,微信小程序挂件辅助,详细插件(果真有挂)进入游戏-大厅左侧-新手福利-...
一分钟了解!新超圣辅助靠谱不,... 一分钟了解!新超圣辅助靠谱不,闲来辅助神器,详细神器(有挂细节)1、玩家可以在新超圣辅助靠谱不线上大...
玩家必备攻略!情怀国粹麻将神器... 玩家必备攻略!情怀国粹麻将神器,皮皮辅助软件演示,详细神器(有挂猫腻)1、进入游戏-大厅左侧-新手福...
重大通报!崇阳斗棋辅助脚本视频... 重大通报!崇阳斗棋辅助脚本视频,免费游戏辅助软件,详细器(今日头条)重大通报!崇阳斗棋辅助脚本视频,...
推荐一款!牵手app破解,来物... 推荐一款!牵手app破解,来物局潮汕辅助,详细脚本(有挂工具)来物局潮汕辅助能透视中分为三种模型:来...
免费测试版!功夫川麻bug,微... 免费测试版!功夫川麻bug,微信小程序途游四川辅助器,详细app(有挂秘笈)微信小程序途游四川辅助器...
专业讨论!德扑之星私人局辅助,... 专业讨论!德扑之星私人局辅助,川游游戏辅助,详细app(有挂规律)1、首先打开德扑之星私人局辅助辅助...