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

相关内容

热门资讯

重大推荐“赣牌圈挂可以安装”从... 重大推荐“赣牌圈挂可以安装”从前有开挂辅助插件(有挂辅助)是一款可以让一直输的玩家,快速成为一个“必...
透视规律!wpk透视挂是真的-... 透视规律!wpk透视挂是真的-分享开挂透视辅助技巧(有挂辅助)1、超多福利:超高返利,海量正版游戏,...
每日必看“werplan脚本”... 每日必看“werplan脚本”从前有开挂辅助工具(的确有挂);亲,有的,ai轻松简单,又可以获得无穷...
透视黑科技!大菠萝789辅助器... 透视黑科技!大菠萝789辅助器下载-了解开挂透视辅助技巧(新版有挂)在进入大菠萝789辅助器下载辅助...
玩家攻略推荐“咸宁方片十三张透... 玩家攻略推荐“咸宁方片十三张透视脚本”本来有开挂辅助神器(有挂教程);亲真的是有正版授权,小编(透视...
透视了解!hhpoker有挂一... 透视了解!hhpoker有挂一直输-专业开挂透视辅助工具(存在有挂)hhpoker有挂一直输软件透明...
玩家亲测“新海贝之城辅助可以设... 玩家亲测“新海贝之城辅助可以设置”从前有开挂辅助软件(有挂方法)是一款可以让一直输的玩家,快速成为一...
透视中牌率!云扑克有透视-必备... 您好,云扑克有透视这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家在...
推荐一款“闲逸辅助器”一直有开... 推荐一款“闲逸辅助器”一直有开挂辅助安装(有挂存在);推荐一款“闲逸辅助器”一直有开挂辅助安装(有挂...
透视真的!拱趴大菠萝十三水透视... 透视真的!拱趴大菠萝十三水透视挂-分享开挂透视辅助app(的确有挂)1、拱趴大菠萝十三水透视挂系统规...