java Android强制停止对应用程序的回调?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11160897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:01:34  来源:igfitidea点击:

Android force Stop callback to application?

javaandroid

提问by Chris

If an app is forced stop by following the steps Settings->Application->ManageApplications -> --> Force Stop , Does android call the onDestroy of the Activity or the Application ? (If not , Is there any way to know if the application has died due to a force Stop triggered by the User ) .

如果按照 Settings->Application->ManageApplications -> --> Force Stop 的步骤强制停止应用程序,android 是否调用 Activity 或应用程序的 onDestroy ?(如果没有,有没有办法知道应用程序是否由于用户触发的强制停止而死亡)。

回答by Alex Lockwood

Force stopping the app will kill the entire process (i.e. with Process.killProcess(int pid)). All resources associated with the application will be removed and freed by the kernel. So no, there is no way that you can intercept this action.

强制停止应用程序将终止整个进程(即使用Process.killProcess(int pid))。与应用程序相关的所有资源都将被内核删除和释放。所以不,你没有办法拦截这个动作。

When you release your application on the market, the developer console will provide you with stats regarding force closes, crashes, etc. (if that is why you are asking).

当您在市场上发布您的应用程序时,开发者控制台将为您提供有关强制关闭、崩溃等的统计信息(如果这就是您询问的原因)。

回答by Mxyk

In short, no, onDestroy()is not called, and you can't do this. Android doesn't support it.

简而言之,不,onDestroy()不被调用,你不能这样做。安卓不支持。

A more extended answer...

一个更广泛的答案......

onDestroy()does not appear to be called in this scenario. I tested this by attempting to have it Toastme before calling super.onDestroy, but the Toastmessage never showed up. (And according to this post, onDestroy()is really unreliable and won't be called often, if at all, on phones, whereas it may be called on an emulator - so be aware of that). Instead killProcess()is called, and we cannot intercept that.

onDestroy()在这种情况下似乎不会被调用。我通过Toast在调用 super.onDestroy 之前尝试拥有它来对此进行了测试,但是该Toast消息从未出现过。(并且根据这篇文章onDestroy()它真的不可靠并且不会经常在电话上调用,如果有的话,在电话上,而它可能会在模拟器上调用 - 所以要注意这一点)。相反killProcess()被调用,我们无法拦截它。

In addition, according to the accepted answer in this post, it appears we can't even catch and perform tasks after a user-controlled force stop.

此外,根据this post中已接受的答案,在用户控制的强制停止,我们似乎甚至无法捕捉和执行任务。

回答by Falmarri

No. This isn't really possible. I haven't looked at the Android code in question, but I would imagine that "force stop" is just calling killon the process ID of your app. So the only way you could intercept that is if you could trap signals, which I don't think Android allows.

不,这真的不可能。我没有看过有问题的 Android 代码,但我可以想象“强制停止”只是调用kill您的应用程序的进程 ID。因此,您可以拦截的唯一方法是您是否可以捕获信号,我认为 Android 不允许这样做。

If you were rooted, you could possibly do this, but not in any standard way.

如果您已扎根,您可能会这样做,但不能以任何标准方式进行。