问题描述
我知道,一个服务的的onDestroy()方法可能永远不会被调用,但有人可以告诉我什么时候可能会发生这种情况?我在它是否可能一个服务被杀死,但其VM将继续运行特别感兴趣。
我问,因为我有在服务的onStartCommand()方法注册ContentObservers并注销它们的onDestroy()的服务。如果是从来没有叫服务的的onDestroy()方法,因为整个虚拟机被杀害,将被罚款(与它创建的观察员一起)。但我不知道是否有可能为一个服务走开没有的onDestroy()被调用,而它创造了将生活,然后继续观察员接受改变。
Here are three off the top of my head:
If the user Force Stops you from the Settings app
If Android needs RAM in a hurry (e.g., to process an incoming phone call) and elects to terminate your process to free up that RAM
You terminate the process from DDMS
Also, if your service crashes with an unhandled exception somewhere, Android may consider the service to be defunct and skip onDestroy()
. I'm not sure about this one, as I haven't specifically tried it.
Other than the unhandled-exception possibility I mention above, I am reasonably certain that if the process will be terminated in the conditions where onDestroy()
is not called.
这篇关于当一个服务的的onDestroy()方法不会被调用的情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!