Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android service stops

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 supposed to do this every 5 minutes.

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.

Now, I've read that the Scheduler will stop if it encounters an uncaught Exception, and I'm sure that's the case as well with the TimerTask - But checking the log files, there aren't any exceptions at all...

When I got home from work today I decided to do an experiment with Handler.

I created 2 handlers, one would run the code for checking the servers and increment an int one at a time. The other would just Log the value of said int, so as to not stop should it encountered an exception (I can't see how it would though).

It runs great, with the usual 1-5 mins inaccuracy, for a few hours and then stops, below is the last lines from the log:

02-07 20:03:25.892 D/dalvikvm(  992): GC_EXPLICIT freed 192K, 53% free 4295K/9031K, external 3755K/4825K, paused 114ms
02-07 20:03:35.572 D/dalvikvm( 5472): GC_EXPLICIT freed <1K, 54% free 2895K/6279K, external 2002K/2137K, paused 61ms
02-07 20:04:42.212 V/ServerChecker(12568): Timer triggered
02-07 20:04:42.212 V/ServerChecker(12568): Checking: linux15
02-07 20:04:44.152 V/ServerChecker(12568): Checked: linux15
02-07 20:04:44.152 V/ServerChecker(12568): Checking: linux1
02-07 20:04:44.462 V/ServerChecker(12568): Checked: linux1
02-07 20:04:44.462 V/ServerChecker(12568): Checking: linux12
02-07 20:04:44.762 V/ServerChecker(12568): Checked: linux12
02-07 20:04:44.762 V/ServerChecker(12568): Checking: linux9
02-07 20:04:45.072 V/ServerChecker(12568): Checked: linux9
02-07 20:04:45.072 V/ServerChecker(12568): Checking: linux14
02-07 20:04:45.382 V/ServerChecker(12568): Checked: linux14
02-07 20:04:45.382 V/ServerChecker(12568): Test timer triggered: 13
02-07 20:05:01.002 E/InputDispatcher(  223): channel '406cefc8 com.n04dev.serverchecker/com.n04dev.serverchecker.ServerChecker (server)' ~ Consumer closed input channel or an error occurred.  events=0x8
02-07 20:05:01.002 E/InputDispatcher(  223): channel '406cefc8 com.n04dev.serverchecker/com.n04dev.serverchecker.ServerChecker (server)' ~ Channel is unrecoverably broken and will be disposed!
02-07 20:05:08.932 D/dalvikvm(12842): GC_EXPLICIT freed 73K, 51% free 2641K/5379K, external 2002K/2137K, paused 37ms
02-07 20:05:09.132 D/dalvikvm(  185): GC_EXPLICIT freed 11K, 53% free 2554K/5379K, external 2002K/2137K, paused 96ms
02-07 20:05:12.022 D/dalvikvm(  185): GC_EXPLICIT freed <1K, 53% free 2554K/5379K, external 2002K/2137K, paused 164ms
02-07 20:05:12.062 D/dalvikvm(  185): GC_EXPLICIT freed <1K, 53% free 2554K/5379K, external 2002K/2137K, paused 36ms
02-07 20:05:18.612 D/dalvikvm(12852): GC_EXPLICIT freed 59K, 52% free 2596K/5379K, external 2002K/2137K, paused 72ms

I've looked up the last two messages concerning my app and found this thread - But I can't see how that would apply in my case.

I really hope you guys have some idea about what I'm doing wrong. I've been at it for weeks.. Trying different timers, having a timer try to catch the "main" timer stopping and then restarting it, but with no luck, and as my last experiment shows, it doesn't seem to be a problem with the timer.. I think..

like image 251
noir04 Avatar asked Feb 07 '11 20:02

noir04


People also ask

How do you stop a service from stopping on Android?

If you really needed Android not to kill your service, your best bet would be to make it a System App, or make your service return START_STICKY in your onStartCommand() method. This way, if your service gets killed, then it will be queued to restart automatically.

What is the life cycle of services in Android?

Lifecycle of Android Services. Android services life-cycle can have two forms of services and they follow two paths, that are: Started Service. Bounded Service.


1 Answers

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).

like image 162
CommonsWare Avatar answered Oct 21 '22 00:10

CommonsWare