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

相关内容

热门资讯

实测揭晓!微扑克职业代打,微扑... 实测揭晓!微扑克职业代打,微扑克有挂么(微扑克)切实存在有挂(有挂解惑)-哔哩哔哩;1、许多玩家不知...
研究成果!微扑克辅助软件,微扑... 研究成果!微扑克辅助软件,微扑克有挂么(微扑克)本来存在有挂(有挂实锤)-哔哩哔哩微扑克辅助软件软件...
终于清楚!微扑克辅助挂,微扑克... 终于清楚!微扑克辅助挂,微扑克如何让系统发好牌(微扑克)确实是有挂(有挂秘籍)-哔哩哔哩1、微扑克如...
重大通报!微扑克ai辅助工具,... 重大通报!微扑克ai辅助工具,微扑克透牌(微扑克)切实有挂(有挂猫腻)-哔哩哔哩进入游戏-大厅左侧-...
普及知识!微扑克有ai吗,微扑... 普及知识!微扑克有ai吗,微扑克如何让系统发好牌(微扑克)本来有挂(存在有挂)-哔哩哔哩1、上手简单...
一分钟教会你!微扑克有挂么,微... 一分钟教会你!微扑克有挂么,微扑克ai机器人(微扑克)好像是真的有挂(真的有挂)-哔哩哔哩1、微扑克...
玩家实测!微扑克辅助工具,微扑... 玩家实测!微扑克辅助工具,微扑克如何让系统发好牌(微扑克)真是存在有挂(详细教程)-哔哩哔哩1、全新...
玩家必看科普!微扑克辅助提高胜... 玩家必看科普!微扑克辅助提高胜率,微扑克透牌(微扑克)原来有挂(有挂神器)-哔哩哔哩微扑克辅助提高胜...
查到实测辅助!微扑克辅助工具怎... 查到实测辅助!微扑克辅助工具怎么下载,微扑克有挂么(微扑克)确实有挂(有挂猫腻)-哔哩哔哩;1、微扑...
最新研发!微扑克有挂么,微扑克... 最新研发!微扑克有挂么,微扑克有挂么(微扑克)果然存在有挂(真的有挂)-哔哩哔哩1)微扑克有挂么辅助...