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

相关内容

热门资讯

我来分享!约局吧游戏挂,wep... 我来分享!约局吧游戏挂,wepoker私人局透视插件,技巧教程(有挂技巧);wepoker私人局透视...
记者发布!wejoker辅助器... 记者发布!wejoker辅助器要钱玩吗,wepoker挂,必赢教程(有挂软件)关于wejoker辅助...
重大科普!we-poker辅助... 1、重大科普!we-poker辅助器,wepoker高级辅助,软件教程(有挂教程)2、进入游戏-大厅...
一分钟揭秘!wpk透视辅助下载... 一分钟揭秘!wpk透视辅助下载,wejoker辅助器要钱玩吗,曝光教程(有挂技巧);1分钟了解详细教...
一分钟揭秘!wepoker网页... 1、一分钟揭秘!wepoker网页版透视方法,hhpoker辅助挂,详细教程(有挂教程);详细教程。...
玩家攻略推荐!poker wo... 玩家攻略推荐!poker world辅助,hhpkoer辅助挂是真的吗,详细教程(有挂透明);实战中...
记者发布!xpoker辅助助手... 1、记者发布!xpoker辅助助手,pokemmo免费脚本,AI教程(有挂透视);详细教程。2、po...
推荐攻略!pokemmo手机版... 推荐攻略!pokemmo手机版修改器,wejoker辅助软件视频,靠谱教程(有挂技巧);最新版202...
最新技巧!德扑HHpoker有... 最新技巧!德扑HHpoker有挂吗,wepoker钻石怎么看底牌,可靠教程(有挂教程);大神普及一款...
今日科普!aapoker插件,... 今日科普!aapoker插件,wepoker开脚本视频,德州论坛(有挂教程);致您一封信;亲爱wep...