首页 > 解决方案 > 如何保持设备传感器唤醒?

问题描述

使用传感器我正在检测一些计算的运动,我在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;
}

Android 系统不会杀死我的应用程序,它会运行一整夜。但是在我打开屏幕之前传感器没有响应。

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

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

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

有没有一种方法可以让手机的传感器长期保持活跃,或者我的前台服务/通知有什么问题?但是,我对具有更高优先级的前台服务进行了很多研究。

标签: javaandroidsensorsandroid-sensorsforeground-service

解决方案


推荐阅读