开发者

Can a service be killed by a task killer

I'm write an auto-speaker app which has a service inside, and list开发者_运维知识库en 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.


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜