当一个服务的的onDestroy()方法不会被调用的情况? [英] Situations when a Service's onDestroy() method doesn't get called?

查看:777
本文介绍了当一个服务的的onDestroy()方法不会被调用的情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,一个服务的的onDestroy()方法可能永远不会被调用,但有人可以告诉我什么时候可能会发生这种情况?我在它是否可能一个服务被杀死,但其VM将继续运行特别感兴趣。

我问,因为我有在服务的onStartCommand()方法注册ContentObservers并注销它们的onDestroy()的服务。如果是从来没有叫服务的的onDestroy()方法,因为整个虚拟机被杀害,将被罚款(与它创建的观察员一起)。但我不知道是否有可能为一个服务走开没有的onDestroy()被调用,而它创造了将生活,然后继续观察员接受改变。


解决方案

  

我知道,一个服务的的onDestroy()方法可能永远不会被调用,但有人可以告诉我什么时候可能会发生这种情况?


下面是三关我的头顶部:


  • 如果强制阻止你设置应用程序用户


  • 如果需要的Andr​​oid在匆忙(例如,处理传入的电话)的RAM和选举终止您的进程腾出内存


  • 您终止从DDMS过程


另外,如果您的服务与未处理的异常崩溃的地方,机器人可以考虑该服务是倒闭,并跳过的onDestroy()。我不知道这一个,因为我没有具体试过了。


  

但我不知道是否有可能为一个服务走开没有的onDestroy()被调用,而它创造了将生活,然后继续观察员接受改变。


除了未处理的异常可能我上面提到的,我有理由相信,如果该进程将在条件被终止,其中的onDestroy()不叫

I'm aware that a Service's onDestroy() method may never be called but can someone tell me when such a scenario might occur? I'm especially interested in whether it's possible for a Service to be killed, yet its VM would continue to run.

I ask because I have a service that registers ContentObservers in the service's onStartCommand() method and unregisters them onDestroy(). If the service's onDestroy() method was never called because the whole VM was killed (along with the observers it created,) that would be fine. But I'm wondering if it's possible for a service to "go away" without onDestroy() being called, while the observers it created would live on and continue to receive changes.

解决方案

I'm aware that a Service's onDestroy() method may never be called but can someone tell me when such a scenario might occur?

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.

But I'm wondering if it's possible for a service to "go away" without onDestroy() being called, while the observers it created would live on and continue to receive changes.

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()方法不会被调用的情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆