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

相关内容

热门资讯

透视好友!wepoker辅助软... 透视好友!wepoker辅助软件视频,抚州麻将牌牌局修改器(其实真的有挂)1、任何抚州麻将牌牌局修改...
透视教程!德州局怎么透视,新5... 透视教程!德州局怎么透视,新518互游脚本(一直有挂);新518互游脚本辅助器中分为三种模型:新51...
透视系统!fishpoker透... 透视系统!fishpoker透视底牌,小程序牵手跑得快辅助器(一直真的是有挂)透视系统!fishpo...
透视实锤!we-poker辅助... 透视实锤!we-poker辅助器,卡农大厅辅助(竟然真的有挂)1、卡农大厅辅助透视辅助简单,卡农大厅...
透视了解!wepoker怎么发... 透视了解!wepoker怎么发冤家牌,科乐辅助功能插件(竟然真的有挂);1、上手简单,内置详细流程视...
透视黑科技!hhpoker辅助... 透视黑科技!hhpoker辅助软件下载,挂机操作微乐(一直真的是有挂)1、点击下载安装,挂机操作微乐...
透视科技!约局吧作弊脚本,新九... 透视科技!约局吧作弊脚本,新九天辅助(一直存在有挂)1、玩家可以在新九天辅助软件透明挂俱乐部对游戏的...
透视规律!wepoker作弊方... 透视规律!wepoker作弊方法,微乐小程序破解(都是是真的有挂)透视规律!wepoker作弊方法,...
透视最新!智星德州辅助译码插件... 透视最新!智星德州辅助译码插件靠谱吗,随意玩有没有辅助(总是是有挂);1、每一步都需要思考,不同水平...
透视软件!如何下载wepoke... 透视软件!如何下载wepoker安装包,新导游正版辅助(本来是有挂)1、许多玩家不知道新导游正版辅助...