tags:

views:

556

answers:

2

I'm trying to run a background task which runs every minute or so for an android application but I am struggling to ensure that the task actually runs every minute. I've tried a variety of approaches from using SystemClock.sleep() to the AlarmManager (both repeating alarm and a fixed alarm) but it seems that unless the device is charging any kind of repeating system has a variable repeat rate once the device is unplugged. Is there any way to run a stable consistently repeating service?

+2  A: 

You do not want to do anything every minute, particularly when unplugged.

CommonsWare
Are you saying it's impossible or just unadvised?
Brother Logic
Strongly inadvisable. There was a Jeff Sharkey presentation on Google I/O, in which he explained that a simple service check every 5 minutes, exhausts as much battery, as the device staying idle for 5 minutes, which effectively cuts your battery life in half. My numbers may be a bit off, but it's somewhere along these lines.
Dimitar Dimitrov
Yeah, I remember figures roughly like that as well. The presentation is online and highly recommended.
CommonsWare
A: 

Have you implemented Timer? It works very well for what I use it for, but then again I haven't worried about the precision at all. For all I know it may be varying a bit but I doubt it. It seems pretty consistent to me.

Edit: I am not liable for your responsible or irresponsible use of this facility ;)

MattC
I'm not quite sure why both of our answers warranted a downvote.
MattC
Especially since they're both very relevant to the question.
MattC