从通知中启动应用程序将其从最近的应用程序历史记录中删除 [英] Starting the app from Notification remove it from recent app history

查看:172
本文介绍了从通知中启动应用程序将其从最近的应用程序历史记录中删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从通知中启动我的一个应用程序时出现问题.它永远不会出现在最近的应用"列表中.

I have a problem in one of my apps when I start it from a notification. It never appears in the "recent app" list.

没有通知,一切都会按预期进行:我启动了该应用程序,导航至该应用程序中,然后退出该应用程序(使用主屏幕按钮或后退按钮),然后长按主屏幕按钮可以返回该应用程序=>确定

Without notifications, every thing works as expected: I start the app, navigate into and when I quit it (with home button or back button) after that I can go back into it with a long press on home button => ok.

问题在我收到通知时开始.如果我从通知中启动应用程序,它将启动正确的屏幕,并且我可以使用该应用程序=>确定.但是,当我退出该应用程序(使用主屏幕或后退按钮)时,它不再出现在最近的应用程序"列表中.更精确地:

The problem starts when I receive a notification. If I start the app from the notification, it starts the correct screen and I can use the app => ok. But when I quit the app (with home or back button) it do not appears anymore in the "recent app" list. More precisely:

  • 如果在从通知中启动应用程序之前,该应用程序已出现在最新应用程序"列表中,则会将其删除
  • 如果该应用程序不在最近使用的应用程序"列表中,则不会添加该应用程序

下面是我在状态栏中添加通知的原始代码:

Below, my original code to add the notification in the status bar:

mNotification = new Notification(R.drawable.ic_notif, message, System.currentTimeMillis());
mNotification.flags |= Notification.FLAG_AUTO_CANCEL;

Intent notificationIntent = new Intent(Intent.ACTION_VIEW,Uri.parse(link));
notificationIntent.putExtra(...);

PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
mNotification.setLatestEventInfo(context, context.getString(R.string.app_name), message, contentIntent);
notificationManager.notify(ConfigApp.NOTIFICATION_ID, mNotification);

我尝试添加 FLAG_ACTIVITY_NEW_TASK ,但这没有帮助:

I tried adding a FLAG_ACTIVITY_NEW_TASK but it did not help:

Intent notificationIntent = new Intent(Intent.ACTION_VIEW,Uri.parse(link));
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
notificationIntent.putExtra(...);

活动的清单声明由通知开始:

Manifest declaration of the Activity started by the notification:

<activity
    android:name=".activity.ActivityMessages"
    android:label=""
    android:windowSoftInputMode="stateHidden">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <data
            android:host="hostapp"
            android:pathPrefix="/prefixapp"
            android:scheme="schemeapp" />
    </intent-filter>
</activity>

在收到通知后,有人知道如何将应用程序保留在最新应用程序"列表中吗?

Does anyone knows how to keep the app in the "recent app" list after being started form a notification?

推荐答案

将操作android.intent.action.MAIN和类别android.intent.category.LAUNCHER添加到您的活动规范中.具有两个动作和类别似乎很容易,但是实际上是Android支持的 a>.

Add the action android.intent.action.MAIN and the category android.intent.category.LAUNCHER to the specification of your activity. Having two actions and categories seem like a hack but it's actually supported by Android.

<activity
    android:name=".activity.ActivityMessages"
    android:label=""
    android:windowSoftInputMode="stateHidden">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
        <data
            android:host="hostapp"
            android:pathPrefix="/prefixapp"
            android:scheme="schemeapp" />
    </intent-filter>
</activity>

那应该将其添加到最近的应用程序"列表中. Android开发者文档中对此原因进行了说明. :

That should add it to the recent applications list. The reason is described in the Android developer documentation:

这种意图过滤器会为 活动显示在应用程序启动器中,为用户提供了 启动活动并返回其创建的任务的方法 启动后的任何时间.

An intent filter of this kind causes an icon and label for the activity to be displayed in the application launcher, giving users a way to launch the activity and to return to the task that it creates any time after it has been launched.

第二项功能很重要:用户必须能够离开任务 然后稍后使用此活动启动器返回. 为此 原因,这两种将活动标记为始终启动的启动模式 任务"singleTask"和"singleInstance"仅应在以下情况下使用 该活动具有ACTION_MAIN和CATEGORY_LAUNCHER过滤器. 例如,想象一下,如果缺少过滤器会发生什么: intent启动一个"singleTask"活动,启动一个新任务,并且 用户花费一些时间来完成该任务.用户然后按 主页按钮.现在,该任务已发送到后台,而不是 可见的.现在,用户无法返回任务,因为它是 没有在应用程序启动器中显示.

This second ability is important: Users must be able to leave a task and then come back to it later using this activity launcher. For this reason, the two launch modes that mark activities as always initiating a task, "singleTask" and ""singleInstance", should be used only when the activity has an ACTION_MAIN and a CATEGORY_LAUNCHER filter. Imagine, for example, what could happen if the filter is missing: An intent launches a "singleTask" activity, initiating a new task, and the user spends some time working in that task. The user then presses the Home button. The task is now sent to the background and is not visible. Now the user has no way to return to the task, because it is not represented in the application launcher.

这篇关于从通知中启动应用程序将其从最近的应用程序历史记录中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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