正在从 android 的最近应用列表中检测应用被终止

Detecting app is being killed from the recent app list in android

我想检测应用是被android系统杀掉还是用户杀了最近应用列表中的应用。如果 activity 以任何一种方式被杀死,我想清除 webview 中的 cookie。 webview 是我的主启动器 activity 中另一个 activity 的一部分。目前,我正在发送广播以在调用启动器 activity 的 onCreate 方法时清除 cookie。但是当我以正常方式关闭应用程序并再次启动它时,cookies 详细信息被清除(广播工作正常),而如果应用程序被最近的应用程序杀死,它似乎没有发送广播,即使onCreate 是 called.Kindly 提示我所缺少的。

It seems you need to clear the cache or cookies on onDestroy or onStop of your main activity.

(Post 一些您遇到问题的代码)

onDestroyonStop 如果应用程序从最近的应用程序关闭或被 android 系统杀死,则不会被触发。

Here 是完成此操作的解决方法。

如果您有任何困惑,请告诉我。

我会建议不同的方法来实现相同的目的。传递数据或一些标志,通知您的 webViewActivity 是否清除您的 cookie 这是可以帮助您的小代码片段

//在你的启动器中 Activity

Intent intent=new Intent(LauncherActivity.this,WebViewActivity.class);
intent.putExtra("clear_cookies",true); 
startActivity(intent)

//在你的webViewActivity的onCreate

Boolean cookieFlag=false;
    Bundle bundle=getIntent().getExtras();
    if(bundle !=null)
    {
        if(bundle.containsKey("clear_cookies"))
        {
            cookieFlag=bundle.getBoolean("clear_cookies")

        }
    }
    if(cookieFlag)
    {
        //write your code clearing cookie
    }