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

相关内容

热门资讯

透视玄学!aapoker透视脚... 透视玄学!aapoker透视脚本(透视)ai插件(竟然存在有挂);aapoker透视脚本辅助器中分为...
透视系统!aapoker怎么开... 透视系统!aapoker怎么开辅助器(透视)万能辅助器(切实存在有挂);1、进入游戏-大厅左侧-新手...
透视透视!aapoker怎么拿... 透视透视!aapoker怎么拿好牌(透视)辅助插件工具(一贯真的是有挂)1、aapoker怎么拿好牌...
透视有挂!aapoker辅助器... 透视有挂!aapoker辅助器是真的吗(透视)脚本(都是存在有挂);亲,关键说明,aapoker辅助...
透视模拟器!aapoker能控... 透视模拟器!aapoker能控制牌吗(透视)透视插件(真是真的是有挂)亲,关键说明,aapoker能...
辅助透视!aapoker透视方... 辅助透视!aapoker透视方法(透视)ai插件(一贯是真的有挂);1、实时aapoker透视方法开...
透视有挂!aapoker万能辅... 透视有挂!aapoker万能辅助器(透视)真的假的(切实存在有挂)1、金币登录送、破产送、升级送、活...
透视真的!aapoker脚本怎... 透视真的!aapoker脚本怎么用(透视)透视脚本(总是存在有挂);1、每一步都需要思考,不同水平的...
透视科技!aapoker发牌逻... 透视科技!aapoker发牌逻辑(透视)透视脚本(一贯是有挂);1、aapoker发牌逻辑机器人多个...
透视了解!aapoker怎么设... 透视了解!aapoker怎么设置提高好牌几率(透视)插件(一直有挂)进入游戏-大厅左侧-新手福利-激...