I am trying to duty cycle the accelerometer on Android phone, but I am not sure what’s the correct way to implement it.
More specifically, I want to assign a duty cycle percentage x% which means that the accelerometer would be activated for x% percent of the time, and rest for 1-x% percent of the time.
I am thinking about using two TimerTasks, but I would like to know other options as well.
Thanks!
You could use Handler.postDelayed() for the timing (where you call postDelayed with delay alternating between the sampling window length and the sleep window length).
For this solution (as well as for the solution with TimerTask!) you will need to keep a Wake Lock because the timers will not wake your device up once it goes into sleep mode.
Thus, if you want to sense in the background this will drain the battery fast! In this case you must use the AlarmManager to wake your device periodically and then acquire a Wake Lock only during the sampling window length.