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

天涯浪子 提交于 2019-12-04 15:49:42

问题


In my application I want to detect the shake event and I'm using SensorEventListener, the code is working fine when my activity is running in foreground. But when I press the lock button of the phone to lock the screen, the shake event can't be detected. I have tested my code on the Samsung gts5360. But the same code is working fine on sony ericssion xperia mini pro. Actually my Samsung phone is not detecting the shake events when I leave the device idle for approx. 45 seconds, after locking the device.

Then I shake the phone, it does not detect the shake, but when I shake phone after several seconds delay it starts listening the shake. This behavior of my samsung phone is not consistent. It starts and stop listening the shake event after a random amount of time.

Now my question is that "Is this the android feature that the device does not detects a shake event when the screen is lock/Off ?".

If it is, then how my samsung phone starts/stops listening after some seconds of locking the phone?

And how it is continuously listening the shake event when screen is lock/off in "Sony ericssion xperia mini pro"?.

Is this feature vary vendor to vendor?

If some body needs my code then let me know,I will provide it.


回答1:


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.



来源:https://stackoverflow.com/questions/9372367/unable-to-detect-shake-event-when-my-phones-screen-is-off-in-android

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