unable to detect shake event when my phones screen is off in android

帅比萌擦擦* 提交于 2019-12-03 09:00:29
Douglas Jones

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();

This permission is needed: "android.permission.WAKE_LOCK"

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!