Search code examples
androidservicesticky

Android Service run indefenitely


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


Solution

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