3

I am developing an application which should detect user movement and when he stops for more than 5 minutes it should sound an alarm. I was able to detect movement with accelerometer but the problem is it doesnt work when the screen is off. I have also tried using partial wakeLock. Re-registering accelerometer doesnt work either (this should be workaround for motorola devices).

Maybe I can do this using GPS and sound an alarm when GPS speed is less than 1m/s for more than 5 minutes but I am not sure if I will receive GPS updates when screen is off.

So I need a solution that will detect user movement even is screen is off on most devices. Any ideas on how to acomplish this?

Thanks in forward

horin
  • 1,664
  • 6
  • 23
  • 52
  • Hello, I have tried using a service as stated in article posted by "tomloprod" but is till doesnt work. Service will run but accelerometer is suspended. This is due to battery saving and it happens on driver level of cellphone – horin Feb 07 '15 at 10:33

2 Answers2

1

You should acquire a partial wake lock for this kind of operation. Use the PowerManager class.

Something like this:

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

You need also this permission in the AndroidManifest.xml:

 <uses-permission android:name="android.permission.WAKE_LOCK" />

Is recommendable using lock.release(); when you're done your work.

EDIT:

Also, this article could be useful for you.

tomloprod
  • 7,472
  • 6
  • 48
  • 66
  • well as I mentioned "I have also tried using partial wakelock" ... it doesnt work with accelerometer – horin Feb 07 '15 at 09:47
  • Excuse my insistence; I read your question entirely. I find it strange that do not work for you. – tomloprod Feb 07 '15 at 09:49
  • I edited my answer with a link to an article which I think is useful for you. – tomloprod Feb 07 '15 at 09:50
  • I will check that article. It looks promising, will report results after testing ... thanks – horin Feb 07 '15 at 09:51
  • hmm it is not working on my galaxy xcover 2 I am getting reregister event on my debug console but still no readings from accelerometer. After a bit of study I think that this can be problem with driver level of some cellphones and there is no workaround for this. Anyway thanks for help – horin Feb 07 '15 at 10:06
  • I'm sorry. I hope somebody can help you with your problem. – tomloprod Feb 07 '15 at 10:09
0

partial wake lock this is what you need to access while your screen is off.

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

thestrongenough
  • 225
  • 2
  • 14
  • Sorry didn't see your comment ...you can refer this link http://stackoverflow.com/questions/9982433/android-accelerometer-not-working-when-screen-is-turned-off – thestrongenough Feb 07 '15 at 10:20