是一款Android服务,保证调用的onDestroy()?是一款、Android、onDestroy

2023-09-13 00:37:54 作者:北巷长歌悠悠

这是一个机器人的活动的生命周期图中并不能保证的onDestroy()将被调用,但这个过程可能会被杀死,该活动被突然删除。在Android的服务的生命周期图并保证的onDestroy()将被调用。所以,我有两个关于这种差异的问题。

The life cycle diagram of an Activity on an android does not guarantee that onDestroy() would be called, but that the process may be killed and the Activity is removed abruptly. The life cycle diagram of a Service on an android does guarantee that onDestroy() would be called. So I have two questions relating to this difference.

首先,如果该服务是同一过程的一部分活动,是服务的onDestroy()调用,虽然活动的onDestroy()不叫?我想不会,因为杀进程认为,操作系统停止其线程并释放其资源。

Firstly, if the Service is part of the same process as the Activity, is the Service onDestroy() called, though the Activity onDestroy() is not called? I would think not, as "killing a process" suggest that the operating system is stopping its threads and releasing its resources.

如果是这样的话,可以在仅提供服务的过程突然被杀害的操作系统?

And if that is the case, can a Service-only-process be abruptly killed by the OS?

推荐答案

我不知道,你所看到的是一个服务保证具有的onDestroy()调用。据我所知,这是情况并非如此。如果你读该文档的这个页面,它描述了条件,使服务能被杀死。所以,如果你问如果它承载两个活动和服务的过程中被杀害,将的onDestroy()算得上是服务(而不是活动),然后答案是否定的;服务的的onDestroy()不一定会被调用。至于是否以服务只处理可突然杀害OS:是的,可以。这尤其是当你有很多工作要做,而你的 onStartCommand 只调用排队的工作要做异步。然后,该服务将花费其大部分时间不是在保护的onCreate onStartCommand 的onDestroy 的方法。

I'm not sure where you're seeing that a Service is guaranteed to have onDestroy() called. As far as I know, this isn't the case. If you read this page of the docs, it describes the conditions in which a service could be killed. So if you're asking if a process which hosts both an activity and service is being killed, will onDestroy() be called on the service (but not on the activity) then the answer is no; a service's onDestroy() will not necessarily be called. As to whether a service-only process can be abruptly killed by the OS: yes, it can. This is especially true when you have a lot of work to do, and your onStartCommand call only queues up the work to do asynchronously. Then the service will be spending the majority of its time not in the protected onCreate, onStartCommand or onDestroy methods.