Can I detect when my service is killed by “Advanced Task Killer”

吃可爱长大的小学妹 提交于 2019-12-12 08:02:18

问题


My app runs a geolocalisation service that the user can active or disactive by a toggleButton. To check the status of the service, I write a boolean in the Shared Preferences. I listen the beginning of the service and the end of it thanks to the onDestroy() of my service.

My problem is that: When the user kill the service with the "advanced task killer", I can't know that the service is killed, the onDestroy is not called !

How can I deal with that?

Thanks for your help.

Florent


回答1:


When a process is killed (using ATK or android's own force stop button, or the function here), it is immediately purged from memory (if the kernel allows it). This means there's no chance for any additional code to run, meaning there is no way to really deal with a "force kill" sent to your application.

If you want to handle this, you have 2 options (that I can think of):

  1. Publish a disclaimer telling users to add your app to the "ignore" list of ATK.
  2. Find some way to maintain functionality without relying on the onDestroy() method.

EDIT:

If you want to check for your process from a list of currently-running processes, look into getRunningAppProcesses().




回答2:


onDestroy()

If you want to restart the service, you can restart in onDestroy().



来源:https://stackoverflow.com/questions/5862045/can-i-detect-when-my-service-is-killed-by-advanced-task-killer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!