android - 从通知启动应用程序将其从最近的应用程序历史记录中删除

标签 android android-intent android-notifications

当我从通知启动某个应用时,我的其中一个应用出现问题。它永远不会出现在“最近的应用”列表中。

没有通知,一切都按预期工作:我启动应用程序,导航到当我退出它(使用主页按钮或返回按钮)之后,我可以通过长按主页按钮返回它 => 好的.

当我收到通知时,问题就开始了。如果我从通知启动应用程序,它会启动正确的屏幕,我可以使用应用程序 => 好的。但是当我退出应用程序(使用主页或返回按钮)时,它不再出现在“最近的应用程序”列表中。更准确地说:

  • 如果在从通知中启动应用之前,该应用已出现在“最近使用的应用”列表中,则会将其移除
  • 如果该应用不在“最近使用的应用”列表中,则不会添加它

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

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但它没有帮助:

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

通知启动的Activity的 list 声明:

<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>

有谁知道如何在启动后通过通知将应用保留在“最近的应用”列表中?

最佳答案

将 Action android.intent.action.MAIN 和类别 android.intent.category.LAUNCHER 添加到您的 Activity 规范中。拥有两个 Action 和类别似乎是一种 hack,但它是 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 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.

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.

关于android - 从通知启动应用程序将其从最近的应用程序历史记录中删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12211707/

相关文章:

android - NavDeepLinkBuilder 没有重定向到目标 fragment

java - 安卓 : Update database in assets folder

java - 语音识别 Android NPE

java - 如何固定图像裁剪的矩形大小?

android - 来自 Intent.ACTION_OPEN_DOCUMENT 提供的 Android URI 的文件上次修改时间

android通知两次启动相同的 Activity

java - 如何让我的 android 应用程序等待 isInteractive() 方法有效地返回 false?

本地图区域自动移动时,Android map v2 变得低质量

android - 如何判断是否有系统对话框出现?

Android onLocationChanged GPSTracker 崩溃