7

My app simply need to detect any motion of the device while screen is off.

I know that accelerometer is used for this task but it don't work while screen is off in all devices. this is a list of devices http://www.saltwebsites.com/2012/android-accelerometers-screen-off

so is there a way of taking accelerometer sensor data while screen is off that works on all devices?

or is there a way to detect motion using another sensors?

Ahmed Hegazy
  • 12,395
  • 5
  • 41
  • 64

4 Answers4

6

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

You can use it like this:

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 obtain accelerometer data in a Service, you could simply acquire lock in it's onCreate() and release in onDestroy().

alex
  • 10,900
  • 15
  • 70
  • 100
  • No, it should work in Activity as well. How do you know it's not working? If you're sure you've got everything right, maybe it's the device specific issue, as Gunaseelan pointed out. – alex Jul 01 '13 at 10:09
  • I am testing on Samsung Galaxy Note II. take a look here http://stackoverflow.com/questions/9982433/android-accelerometer-not-working-when-screen-is-turned-off on the answer it's why I asked the question :) – Ahmed Hegazy Jul 01 '13 at 10:14
  • I don't know the list of devices where there is a bug, but it works on my Samsung Galaxy SII. So I suppose it should work for you as well... Please, double check your code. – alex Jul 01 '13 at 10:16
  • @7agooz you need to put `acquire()` in `onResume()` or `onStart()` and put `release()` in `onPause()` or `onStop()`. You've got it the other way round. – alex Jul 01 '13 at 10:36
  • @7agooz alright, so my guess now is that the app's main UI Thread is being put to sleep after locking the phone. In which case Service is your last shot. Give it a try, making this kind of calculations in UI Thread is no good idea anyway. – alex Jul 01 '13 at 10:50
  • @alex even same prob with my GTN7100 ... but it works fine on other devices. – Sharanabasu Angadi Dec 16 '13 at 15:50
3

Yes you can use accelerometer in the background or when screen is off
but you need to hold a WakeLock [Link] to prevent the device from sleeping.

If you need to detect if the device is still or if it started moving again you might be interested in Recognizing the User's Current Activity from Google Services.

Plato
  • 2,338
  • 18
  • 21
  • WaveLock doesn't work . Is "Recognizing the User's Current Activity" relies on internet connection or it's a sensor thing? – Ahmed Hegazy Jul 01 '13 at 10:18
  • It shouldn't need an internet connection, but I'm not 100% sure. It need the Google Play Services to be installed on hte device though. – Plato Jul 01 '13 at 10:28
1

After a quick research I found that most of android devices does not send the accelarometer events when the screen is off. To know more about this bug please take a look on here. Also here too.

Community
  • 1
  • 1
Gunaseelan
  • 14,415
  • 11
  • 80
  • 128
0

When the screen is off the CPU goes to sleep and you cannot capture events without taking a partial wakelock. I suggest you to take a partial wakelock when you call the onPause and release it onResume. Be careful with wakelocks they make your phone drain a really big amount of energy, you should manage them carefully.

PS: You have to acquire the wakelock in the onPause method because if you try to call it somewhere else you might not be able to gain it because the CPU may be already shutted down.

Vittorio Cozzolino
  • 931
  • 1
  • 14
  • 31