Constantly monitor the sensor in Android

I'm trying to figure out how best to control an accelerometer sensor with a polling rate of less than 0.25 milliseconds. I have implemented a user interface parameter so that the user can switch to a constant monitoring state and save him from the consequences of a battery leak. Will the remote service work best on the daemon thread due to how Android handles memory and thread cleanup? The bottom line is to control the accelerometer as close to the maximum battery capacity as possible. And this monitoring should be long, maybe even more than 24 hours in a row, again I understand the consequences of energy consumption. Any suggested snippets or snippets of code will be appreciated.

Just newbe is looking for advice from the wisdom of the Android community. Thanks in advance,

-Steve

COMPLETION: I am trying to detect the moment when a change in acceleration occurs. My code distinguishes between the axis, but getting real-time data from the accelerometer is my goal.

+6
android multithreading daemon
source share
2 answers

We did this using Android Services - they can be launched from activity, but they remain in the background. This is probably what you are looking for!
Some Howtos:

+2
source share

Using a specific thread for monitoring and waiting is the best solution that gives you flexibility in the waiting period. This is quite effective as it does not require any particular service.

class MonitorThread extends Thread { ... public void run() { for (;;) { long ms = 0; int nanos = 250000; ... // Do something or compute next delay to wait try { Thread.sleep(ms, nanos); } catch (InterruptedException ex) { } } } } 

See http://developer.android.com/reference/java/lang/Thread.html

You set a very short delay (.250 ms), so this will be the processor intensity. You can probably use the result of the accelerometer to increase or decrease this delay. For example, if you find that there is no acceleration, increase the delay (this is for you, but 100 ms seems reasonable or even higher). As soon as you find something, reduce the delay. It all depends on your application.

+1
source share

All Articles