简体   繁体   English

以编程方式从最近的应用程序中删除应用程序

[英]Remove app from recent apps programmatically

I know that Activities can be declared in manifest as being excluded from recents with android:excludeFromRecents : http://developer.android.com/guide/topics/manifest/activity-element.html#exclude我知道活动可以在清单中声明为从最近使用android:excludeFromRecents排除: http : //developer.android.com/guide/topics/manifest/activity-element.html#exclude

However, that's not what I'm looking for, I would like to know if there is a way to remove the app from recent apps programmatically但是,这不是我要找的,我想知道是否有办法以编程方式从最近的应用程序中删除该应用程序

Yes, generally when you want to have special properties for an Activity when starting it you supply special flags to the Intent . 是的,通常当您想要在启动时为Activity提供特殊属性时,您需要为Intent提供特殊标志。 In this case FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS . 在这种情况下, FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS

Updated: 更新:

If you need to hide the current already running activity, you might be able to use this flag in combination with FLAG_ACTIVITY_CLEAR_TOP which would send the new Intent to the existing Activity . 如果您需要隐藏当前已在运行的活动,您可以将此标志与FLAG_ACTIVITY_CLEAR_TOP结合使用,这将把新Intent发送到现有的Activity You'll have to think and perhaps experiment with what happens as the user moves around your stack though and whether that will make your app re-appear in the recent apps. 您将不得不考虑并尝试使用用户在堆栈中移动时会发生什么,以及是否会使您的应用重新出现在最近的应用中。

I did it in easiest way: 我以最简单的方式做到了:

Add New Activity : 添加新活动

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 exitApplicationAndRemoveFromRecent(Context context)
    {
        Intent intent = new Intent(context, ExitActivity.class);

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

        context.startActivity(intent);
    }
}

Now exit from other Activity : 现在退出 其他活动

@Override
public void onBackPressed() {
 ExitActivity.exitApplicationAndRemoveFromRecent(OtherActivity.this);
 super.onBackPressed();
}

Done . 做完了

This can be done using the ActivityManager.AppTask functionality (starting in API 21) 这可以使用ActivityManager.AppTask功能完成(从API 21开始)

    ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
    if(am != null) {
        List<ActivityManager.AppTask> tasks = am.getAppTasks();
        if (tasks != null && tasks.size() > 0) {
            tasks.get(0).setExcludeFromRecents(true);
        }
    }

Following is the definition of the flag android:excludeFromRecents (which i know you have already seen): 以下是标志android:excludeFromRecents的定义(我知道你已经看过):

Whether or not the task initiated by this activity should be excluded from the list of recently used applications ("recent apps"). 是否应从最近使用的应用程序列表(“最近的应用程序”)中排除此活动启动的任务。 That is, when this activity is the root activity of a new task, this attribute determines whether the task should not appear in the list of recent apps. 也就是说,当此活动是新任务的根活动时,此属性确定该任务是否不应出现在最近的应用列表中。 "true" if the task should be excluded from the list; 如果任务应从列表中排除,则为“true”; "false" if it should be included. “假”如果应该包括在内。 The default value is "false". 默认值为“false”。

so to remove the app from the list of recent app you can set this flag on the first activity in your application since that activity launches the the task for you application. 因此,要从最近的应用程序列表中删除应用程序,您可以在应用程序的第一个活动上设置此标志,因为该活动会启动应用程序的任务。 If you have multiple tasks (unlikely for most apps) in your application then you need o set this flag for root activity of all the task. 如果您的应用程序中有多个任务(大多数应用程序不太可能),那么您需要为所有任务的根活动设置此标志。

Add these lines to the Activity from which you are exiting the application: 将这些行添加到退出应用程序的Activity:

@Override
public void finish() {
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        super.finishAndRemoveTask();
    }
    else {
        super.finish();
    }
}

在您的活动完成后调用此应该关闭,并且应该完成任务作为完成任务的根活动的一部分。

finishAndRemoveTask();

After receiving the other answers, I managed to get to the following workaround: I have an Activity , let's call it XPTO, declared in manifest with 收到其他答案后,我设法得到以下解决方法:我有一个Activity ,让我们称之为XPTO,在清单中声明

and basically, when I want app to disappear from the recents list, I finish all other activities, then start the XPTO, which basically kills the app (calling android.os.Process.killProcess(android.os.Process.myPid()); in its onResume() 基本上,当我希望应用程序从最近的列表中消失时,我完成所有其他活动,然后启动XPTO,它基本上杀死了应用程序(调用android.os.Process.killProcess(android.os.Process.myPid());在其onResume()

If anyone has a better way of doing this, please share it 如果有人有更好的方法,请分享

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM