且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

当我的手机屏幕在android系统中关闭时,无法检测到震动事件

更新时间:2023-02-26 21:26:54

问题是,很长一段时间以来,一直没有统一的标准来确定屏幕关闭时如何处理传感器.有些设备允许它继续工作,而另一些则不能.最终,Android团队决定,要使其正常工作,应用程序应为此操作获取部分唤醒锁:

The problem is for a long time there has been no consistent standard for what to do with sensors when the screen goes off. Some devices allow it to keep working and others do not. Eventually the Android team decided that for it to work an application should acquire a partial wake lock for this kind of operation:

PowerManager pm = (PowerManager)getSystemService(POWER_SERVICE);
PowerManager.WakeLock lock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "SensorRead");
lock.acquire();

需要此权限:"android.permission.WAKE_LOCK"

您需要确保在完成唤醒锁后将其释放,以便CPU可以完全进入睡眠状态.

You need to make sure you release the wake lock when you are done with it so the CPU can go fully to sleep.

即使有所有这些,它也可能无法正常工作.我发现LG手机最近不太可能支持背景传感器.同样,许多摩托罗拉手机不需要唤醒锁,而只需要在屏幕关闭时重新注册传感器即可.

Even with all of this it may not work. I've found that LG phones most recently are less likely to support background sensors. Also many Motorola phones don't require a wake lock but instead just need to re-register for the sensor when the screen goes off.