2

我有一个在自己的进程中运行的服务。晚上看起来不错,但在我入睡后,我认为 Android 会对其进行处理。

我认为onDestroy()当Android杀死服务时不会调用它是对的吗?如果没有,是否还有其他记录杀戮的地方?

我想我需要研究一下AlarmManager

4

2 回答 2

3

我有一个在自己的进程中运行的服务。

为什么它在自己的过程中?

我认为当 Android 终止服务时不会调用 onDestroy() 是否正确?

它可能,它可能不会。

如果没有,是否还有其他记录杀戮的地方?

不。

我想我需要研究一下AlarmManager。

用户讨厌永远存在的服务,这就是任务杀手流行的原因。他们更讨厌在自己的进程中运行的服务,因为它们消耗额外的 RAM、CPU 和电池,通常没有充分的理由。

如果您的目标是定期做某事,使用AlarmManager-- 这就是它存在的原因。

于 2011-12-03T16:06:25.900 回答
1

根据开发者文档中的Service生命周期,该onDestroy()方法会在服务被杀死或停止时被调用。

另外,如果一个应用程序有一个在后台运行的服务(即在后台播放的音乐播放器),系统会认为该应用程序处于活动状态并且不会杀死它的进程,除非极端条件(我不认为它发生在实践)。

文件说:

Note this means that most of the time your service is running, it may be killed by the system if it is under heavy memory pressure. If this happens, the system will later try to restart the service. An important consequence of this is that if you implement onStartCommand() to schedule work to be done asynchronously or in another thread, then you may want to use START_FLAG_REDELIVERY to have the system re-deliver an Intent for you so that it does not get lost if your service is killed while processing it. 

在这里,我在想的是,如果一个服务被操作系统杀死,操作系统会在稍后尝试重新启动它。但是,在这种情况下,是否调用了 onDestroy() 方法?我不确定。有人对此进行过测试吗?

于 2011-12-03T15:50:54.430 回答