Service that survives Task Killers like Pandora / How do Services actually work?

2.5k views Asked by At

First off, this is not aimed at perpetuating bloatware and other bad practices. I know many will say "don't do that" but I promise to respect users.

I am building an app where I would like a service to run indefinitely. A couple questions:

  1. Are services listed in Advanced Task Killer? Or just Activities?

  2. What's with the services that are always running (Settings -> Apps -> Running Services) like Skype, Swype, etc? I realize Skype could be a process with higher permissions than any Market apps (like packaged into kernel by Verizon or HTC), but Swype was something I installed (Beta for Incredible). So how does that manage to always run in the background? Furthermore - these items are not listed by Advanced Task Killer.

  3. How does Pandora work. It keeps running even when you Task Kill it? Furthermore it's not re-launching itself because the music keeps playing smoothly.

Ideally I would like to build a Pandora-esque app where the users explicity hit the OFF button, but it will survive trigger happy task killers like myself. This would be a feature users are aware of / would agree to.

1

There are 1 answers

3
CommonsWare On BEST ANSWER

I am building an app where I would like a service to run indefinitely.

Wherever it can be avoided, this is not recommended. By default, it doesn't work, as Android gets more and more aggressive with each passing release about stopping this practice for non-foreground services (see below).

So how does that manage to always run in the background?

Swype is an input method editor. As such, even if something shuts it down, it will start up again shortly thereafter, to serve EditText widgets. I imagine that Android might not bother shutting down this service since it is reasonably likely to need it again in short order, but that's just a guess.

How does Pandora work. It keeps running even when you Task Kill it?

Task killers were de-fanged on Android 2.2, so perhaps the problem is that the task killer simply is not working on your device.

Beyond that, you would have to ask the authors of Pandora, or Advanced Task Killer, neither of whom necessarily are monitoring StackOverflow.

Ideally I would like to build a Pandora-esque app where the users explicity hit the OFF button, but it will survive trigger happy task killers like myself. This would be a feature users are aware of / would agree to.

Use startForeground(), to trade off Android not shutting you down (which otherwise will happen) with having a Notification visible so the user remembers "oh, yes, I did agree to let this service run indefinitely, because it is constantly delivering value to me, rather than just being the product of some sloppy coder who couldn't be bothered to use AlarmManager". Also, usually that Notification gives the user easy access to your aforementioned OFF button.