Is movement detectable when the screen is off?

My application just needs to detect any movement of the device when the screen is off.

I know that an accelerometer is used for this task, but it does not work while the screen is off on all devices. this is a list of devices http://www.saltwebsites.com/2012/android-accelerometers-screen-off

Is there any way to get the accelerometer sensor data when the screen is off, which works on all devices?

or is there a way to detect movement using other sensors?

+4
android accelerometer android-sensors
Jul 01 '13 at 9:15
source share
4 answers

Partial Wake Lock is all you need to access the accelerometer when the screen is off.

You can use it as follows:

 private PowerManager.WakeLock mWakeLock; PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag"); mWakeLock.acquire(); 

And after you're done, just release the lock:

 mWakeLock.release(); 

If you get the accelerometer data in Service , you can just get onCreate() lock in it and release it in onDestroy() .

+4
Jul 01 '13 at 9:36 on
source share

Yes, you can use the accelerometer in the background or when the screen is off
but you need to hold WakeLock [Link] so that the device does not sleep.

If you need to determine if the device remains or if it has started to move again, you might be interested in "Recognizing the current user activity" from Google Services.

+1
Jul 01 '13 at 9:27
source share

After a quick research, I found that most Android devices do not send accelerator events when the screen is off. To learn more about this error, check here . Also here .

+1
Jul 01 '13 at 9:28
source share

When the screen is off, the processor goes into sleep mode and you cannot record events without doing a partial wakelock. I suggest you take a partial wakelock when you call onPause and release it onResume . Be careful with wakelocks, they make your phone consume really large amounts of energy, you must carefully monitor them.

PS: You have to purchase wakelock in the onPause method, because if you try to call it elsewhere, you may not be able to get it, because the CPU may already be closed.

0
Jul 01 '13 at 10:22
source share



All Articles