1

I notice that applications like Skype use a service which basically runs 24x7, without getting killed at all. You cannot even manually kill it using task killers ( you can kill them by going to running services and kill service ). How is this implemented?

I find that in Android 2.3, my service gets killed after running for sometime. onDestroy() is never called even if I start the service with START_STICKY. However this works fine on my 2.1 device, that is the service doesnt get killed.

Thanks

1 Answer 1

3

How is this implemented?

Based on the Skype screenshots that show a Notification icon, then they are most likely using startForeground().

I find that in Android 2.3, my service gets killed after running for sometime.

That is perfectly normal.

First, most Android applications do not really need a service that "basically runs 24x7". Users do not like such services, which is why task killers and the Running Services screen and the auto-kill logic in the OS exist. The only reason a service should be running "24x7" is if is delivering value every microsecond. VOIP clients, like Skype, will deliver value every microsecond, as they are waiting for incoming phone calls. Most Android applications do not meet this criterion.

If your service is running constantly, but for a user-controlled period (e.g., a music player), startForeground() is a fine solution.

Otherwise, I would rather that you find a way to eliminate the service that "basically runs 24x7", switching to a user-controllable polling system using AlarmManager, so your service is generally not in memory except when it is delivering value.

4
  • So that Service that uses startForeground(), doesn't that get killed when the Activity process that started it gets killed?
    – nhaarman
    Commented Jul 5, 2011 at 13:26
  • @Niek: If the process is killed, then yes, the Service goes along with it. However, since the service is part of the foreground, task killers can no longer kill it (as of Android 2.2, or perhaps 2.1). But, if the user goes into Settings, they can stop the service or the whole process if they choose. The OS will also still stop the service after a while -- my preferred audiobook reader uses startForeground() and it goes away after a couple of days of inactivity, and I assume that's the OS doing that. Commented Jul 5, 2011 at 13:28
  • @CommonsWare. I've implemented the AlarmManager service in my application and now the app seems to be happy. 24 hours and counting and still the service is alive ( or maybe was killed and was restarted! ) Commented Jul 6, 2011 at 11:59
  • @Mahadevan S: The point of AlarmManager is for the service to GO AWAY in between alarms, after it has done its bit of work related to the last alarm. Commented Jul 6, 2011 at 12:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.