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透视辅助方法,有哪些免费的wpk作弊码-确实有辅助技巧(哔哩哔哩)1、首先打开辅助器...
透视普及!hhpoker有没有... 透视普及!hhpoker有没有辅助,hhpoker辅助-其实是有辅助教程(哔哩哔哩)1、免费辅助多个...
透视科普!aapoker透视方... 透视科普!aapoker透视方法,aapoker公共底牌-果然真的是有辅助攻略(哔哩哔哩)透视科普!...
透视开挂!wpk有作弊吗,wp... 透视开挂!wpk有作弊吗,wpk作弊最怕三个东西-总是一直都是有辅助方法(哔哩哔哩)1、玩家可以在线...
透视必备!德州机器人代打脚本,... 透视必备!德州机器人代打脚本,智星德州插件怎么下载-切实存在有辅助技巧(哔哩哔哩)1)智星德州插件怎...
透视解迷!德普之星辅助器怎么用... 透视解迷!德普之星辅助器怎么用,德普之星辅助软件-竟然一直总是有辅助技巧(哔哩哔哩)1、公共底牌简单...
透视解密!wpk作弊最怕三个东... 透视解密!wpk作弊最怕三个东西,wpk控制牌是真的吗-好像一直总是有辅助软件(哔哩哔哩)1、有没有...
透视曝光!德普之星透视辅助插件... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】很多玩家在这款游戏中打...
透视有挂!wpk辅助购买,wp... 透视有挂!wpk辅助购买,wpk作弊-好像一直总是有辅助方法(哔哩哔哩)透视方法中分为三种模型:靠谱...
透视关于!wpk官网下载链接,... 透视关于!wpk官网下载链接,wpk俱乐部是真的吗-一贯存在有辅助教程(哔哩哔哩)运辅助工具,进入游...