关闭应用程序并从最近的应用程序中删除/ [英] Close application and remove from recent apps/

查看:121
本文介绍了关闭应用程序并从最近的应用程序中删除/的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题很常见,并且在Stack Overflow上问了很多遍,但是在访问了几乎四页的搜索引擎结果以及关于此问题的将近20个Stack Overflow问题之后,我发现没有一个问题得到解决或回答正确.

我想要的东西:

我想在运行时在最近的应用程序列表中显示我的应用程序,但是当我关闭应用程序时,应该终止我的进程,并且应该从最近的应用程序列表中删除该应用程序.

我找到了一些答案:

use System.exit(0); //doesn't clear app from recents
OR
use android.os.Process.killProcess(android.os.Process.myPid()); //doesn't clear app from recents
OR
use finish() or this.finish() or Activity.finish();// doesn't clear app from recents

我在每个答案中看到的一个常见建议是,在清单中添加以下代码:

android:excludeFromRecents //I think this is wrong approach.

由于添加了此内容,当用户在我的应用程序运行时按下主屏幕按钮时,该用户无法在最近的应用程序列表中看到我的应用程序.

关于此的许多其他建议,但是它们都不能同时关闭应用程序和从最近的应用程序列表中清除应用程序.另外,如果您进入Settings>Apps>yourApp>your Application,仍然看到它仍然要求强制停止",这意味着应用程序正在运行!

解决方案

我的解决方案基于上述客人,以及以下gilsaints88的评论(出于Android L兼容性):

将此活动添加到您的AndroidManifest.xml文件中:

<activity
    android:name="com.example.ExitActivity"
    android:theme="@android:style/Theme.NoDisplay"
    android:autoRemoveFromRecents="true"/>

然后创建一个类ExitActivity.java:

public class ExitActivity extends Activity
{
    @Override protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        if(android.os.Build.VERSION.SDK_INT >= 21)
        {
            finishAndRemoveTask();
        }
        else
        {
            finish();
        }
    }

    public static void exitApplication(Context context)
    {
        Intent intent = new Intent(context, ExitActivity.class);

        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);

        context.startActivity(intent);
    }
}

然后,每当您要强制关闭应用程序并将其从最新列表中删除时,请致电:

ExitActivity.exitApplication(context);

此解决方案对我有效,而不是声明android:excludeFromRecents ="true",因为我希望用户能够在最近列表中看到该应用,直到我的应用触发以编程方式关闭该应用为止.

I know this question is common and asked many times on Stack Overflow, but after visiting nearly the four pages of search engine results and nearly the 20 question of Stack Overflow regarding this issue, I found that none of them is resolved or answered correctly.

What I want:

I want to show my app in recent apps list when it is running but when I close app then my process should be killed and application should be removed from recent apps list.

Some answers I found:

use System.exit(0); //doesn't clear app from recents
OR
use android.os.Process.killProcess(android.os.Process.myPid()); //doesn't clear app from recents
OR
use finish() or this.finish() or Activity.finish();// doesn't clear app from recents

And one common suggestion I see in every answer that add below code in manifest:

android:excludeFromRecents //I think this is wrong approach.

because after adding this when user presses home button while my app is running then user cannot see my app in recent Apps List.

And many other suggestions on this but none of them do both tasks close application and clear application from recent apps list. Also if you go in Settings>Apps>yourApp>your Application see that still it asks for "force stop" means application is running!

解决方案

I based my solution on guest's above, as well as gilsaints88's comments below (for Android L compatibility):

Add this activity to your AndroidManifest.xml file:

<activity
    android:name="com.example.ExitActivity"
    android:theme="@android:style/Theme.NoDisplay"
    android:autoRemoveFromRecents="true"/>

Then create a class ExitActivity.java:

public class ExitActivity extends Activity
{
    @Override protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        if(android.os.Build.VERSION.SDK_INT >= 21)
        {
            finishAndRemoveTask();
        }
        else
        {
            finish();
        }
    }

    public static void exitApplication(Context context)
    {
        Intent intent = new Intent(context, ExitActivity.class);

        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);

        context.startActivity(intent);
    }
}

Then whenever you want to force close your application and remove it from the recent's list, call:

ExitActivity.exitApplication(context);

This solution works for me instead of declaring android:excludeFromRecents="true" because I want the user to be able to see the app in the recents list UNTIL the point where my app triggers closing the app programmatically.

这篇关于关闭应用程序并从最近的应用程序中删除/的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆