I\'ve come to the point where I\'m a few steps away from going bald...
I\'m trying to run a service that will check if the servers at my work are running. It\'s supp
I've been through TimerTask, ScheduledExecutorService and finally Handler. They all work "fine" for a few hours, except for some inaccuracy, 1-5 mins off, and then suddenly the "timer" just stops firing.
Of course. None of those are designed for your purpose, as neither keep the device awake, and neither play nicely with the Android framework. Having a service in RAM 24x7 just to mark the passage of time is wasteful, and Android gets more aggressive with each passing release at shutting down everlasting services like this one.
Use AlarmManager
to set up your schedule and an IntentService
to do the actual work. The IntentService
automatically gives you your background thread for your network I/O, and it automatically shuts down the service when the work is complete.
If you intend to have this occur even if the device falls asleep, use a _WAKEUP
alarm with AlarmManager
and my WakefulIntentService (or something along those lines).