2

I'm write an auto-speaker app which has a service inside, and listen to the phone state change.

I expect that the service should be exist all the time and can't be killed.

However, I found that if I use some task killer to kill my app, there will be few seconds my listener won't work.

Although my app seems to be restart automatically in few seconds later and work again.

Why the service could still be killed by task killer??

Is a service just like an activity, just with no UI, and able to restart automatically?

Thanks for your answer.

dong221
  • 3,390
  • 6
  • 29
  • 31

3 Answers3

3

You cannot exempt your service from being ended by the user or automatically by the OS when resources are needed for that matter.

The best you could do is attempt to write code to compensate for restarts but certainly don't write as if your service will run forever without exception.

EDIT

Android Developer documentation Managing the Lifecycle of a Service has useful information on recovering from stops and when the callback hooks get called and not.

Quintin Robinson
  • 81,193
  • 14
  • 123
  • 132
  • Could you tell me how do I compensate this issue, and avoid the blank of that few seconds? thanks. – dong221 Mar 18 '11 at 17:20
  • @dong221 There is not really anything you can do to recoup the time lost, be it a few seconds or longer depending on the shutdown type. I would recommend reading the Android Developer documentation on the Service Lifecycle, I will edit my answer with a link. – Quintin Robinson Mar 18 '11 at 17:22
2

Add this trick to your service onCreate:

startForeground(R.string.app_name, new Notification());

It will prevent service from closing on low memory, task killing etc..

deviant
  • 3,539
  • 4
  • 32
  • 47
0

Because taking away the ability of the user to end a process when he/she wants to is just wrong. Instead try to write a work-around so in the event of of the service shutting down, XXXXXX takes place.

Xezuka
  • 1,011
  • 1
  • 8
  • 10