0

I'm building an Android application which uses accelerometer sensor and sends the value of x-axis, y-axis and z-axis to my server, but the problem is when the screen turns off, accelerometer stops working or in other words stops sending data.

How can I fix this? I'm newbie to Android application development.

Mike Laren
  • 8,028
  • 17
  • 51
  • 70
Abhie
  • 1
  • 3
  • possible duplicate of [Accelerometer stops delivering samples when the screen is off on Droid/Nexus One even with a WakeLock](http://stackoverflow.com/questions/2143102/accelerometer-stops-delivering-samples-when-the-screen-is-off-on-droid-nexus-one) – user1438038 Jun 26 '15 at 08:25
  • I dont know whether it'a duplicate but still no answer – Abhie Jun 26 '15 at 08:36
  • It depends on the hardware you are using (see [list of devices](http://www.saltwebsites.com/2012/android-accelerometers-screen-off)). – user1438038 Jun 26 '15 at 09:14

2 Answers2

0

You should keep CPU on in your device. for do that you have to add this permission in your manifest.

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

and add this code to your code.

PowerManager powerManager = (PowerManager)getSystemService(POWER_SERVICE);
WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
    "MyWakelockTag");
wakeLock.acquire();

For more information about this, please refer to this document. Keep the CPU On

Hamidreza Shokouhi
  • 973
  • 1
  • 12
  • 24
-1

You may try using a Service to run the accelerometer in the backround. So when the screen turns off, the accelerometer would still give you current values.

  • This qualifies as a link-only answer, please include the relevant bits from the link so the answer is valuable even when the link dies. – jessehouwing Jan 28 '16 at 17:15