开发者

android how to handle when a service is killed by OS or taskmanager

I have a service that starts on boot on my android device,the service is started using startService command so it runs separate开发者_运维技巧ly from the UI, I would like to be notified when the service is killed from the OS or from the task manager so I can be able to safely save my data, I have override the onDestroy method but when the service is killed by the task manager the code on the onDestroy method is not executed!

Can I be notified when someone tries to kill my service and how?


I think when your application is killed using kill -9 by the task manager or the system, the dalvik process is destroyed completely without any methods called on it, just like if you call kill -9 on unix process he will be killed immediately without a chance to recover.

When user uses the task manager he should know that. Regarding of killing by the system, i'm pretty sure that unless the system urgently needs to free resources it should end your process gracefully and then your onDestroy should be called.

Did you actually see the application destroyed by the system (say by opening almost all the apps on the phone) without calling onDestroy or only the task manager ?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜