【问题标题】:How to check when the app is killed in android nougat?如何检查应用程序何时在 android nougat 中被杀死?
【发布时间】:2017-11-20 08:15:18
【问题描述】:

我想知道我的应用在后台时是否被杀死。我正在使用 android nougat 设备进行测试。但是当我通过滑动杀死我的应用程序时,我的服务的 onTaskRemoved() 没有调用。

有什么方法可以知道用户在调用的活动的 onDestroy() 之前何时杀死了应用程序?

【问题讨论】:

  • 当应用程序在后台并且由于内存不足而被操作系统杀死时,操作系统会杀死应用程序并调用应用程序的onDestroy。就我对生命周期的了解而言,通过滑动应用程序来杀死应用程序,不会调用回调方法。
  • 不,如果你的整个进程都被杀死了,只有刺客知道这一点
  • @AbdulWaheed android.app.Application 没有onDestroy 方法
  • @pskink 是的,我知道这一点,我牢记这一点,如果应用程序在后台,则必须将某些活动加载到进程的内存中。并且该活动的 onDestroy 将被调用。对不对?
  • @AbdulWaheed 不,Activity#onDestroy 文档说:"Note: do not count on this method being called as a place for saving data! [...] There are situations where the system will simply kill the activity's hosting process without calling this method (or any others) in it, so it should not be used to do things that are intended to remain around after the process goes away."

标签: android activity-lifecycle


【解决方案1】:

onTaskRemoved() 方法仅在您在清单文件中设置android:stopWithTask="false" 时调用。

所以请检查您的清单文件,因为每当服务被系统终止时,总是会调用 onTaskRemoved()。

【讨论】:

  • 我试过 stopWithTask 错误。有时 onTaskRemoved() 正在调用。但并非一直如此。
  • 你能详细说明它什么时候被调用,什么时候不被调用吗?
  • 其实我是用小米红米note4设备测试的。在我努力启用安全中的自动启动活动之前。所以这个 onTaskRemoved() 没有调用。现在它工作正常。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-13
  • 1970-01-01
  • 1970-01-01
  • 2018-03-18
相关资源
最近更新 更多