有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何保持设备传感器处于唤醒状态?

使用传感器,我检测一些计算的运动,我在Service上注册传感器

sensorManager.registerListener(sensorListener, sensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION), SensorManager.SENSOR_DELAY_NORMAL);

为此,我已经在使用带有通知的前台服务来保持传感器处于活动状态

下面是我在Service类上使用的代码(对于返回粘性的通知具有更高的优先级)

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    registerSensor();

 Notification notification = new NotificationCompat.Builder(this, ServiceNotification.CHANNEL_1_ID)
           .setSmallIcon(R.drawable.ic_stat_name)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            //.setCategory(NotificationCompat.CATEGORY_SERVICE)
            //.setOngoing(true)
            //.setAutoCancel(true)
            //.setOnlyAlertOnce(true)
            .setColor(getResources().getColor(R.color.colorPrimaryDark))
            .setContentIntent(pendingIntent)
            .build();

    startForeground(13, notification);
    return START_STICKY;
}

安卓系统不会杀死我的应用程序,它会运行一整晚。但在我打开屏幕之前,传感器不会响应

尝试获取Wakelock解决了我的问题

 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MService: WakeLock");
    mWakeLock.acquire();

但是我不想用WakeLock这么长时间

有没有一种方法可以让手机的传感器长期处于活动状态,或者我的前台服务/通知有什么问题? 然而,我研究了很多具有更高优先级的前台服务


共 (0) 个答案