【问题标题】:Starting the app from Notification remove it from recent app history从通知启动应用程序将其从最近的应用程序历史记录中删除
【发布时间】:2012-08-31 08:36:54
【问题描述】:

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

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

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

  • 如果在从通知中启动应用程序之前,该应用程序存在于“最近的应用程序”列表中,则会将其删除
  • 如果该应用不在“最近使用的应用”列表中,则不会添加它

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

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的清单声明:

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

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

【问题讨论】:

  • 这发生在哪个安卓版本上?
  • 如果删除 &lt;category android:name="android.intent.category.DEFAULT" /&gt; 会发生什么?另外,请尝试删除 intent-filter 组。
  • @WebnetMobile.com 自 2.2 以来的每个版本
  • @Eric 因为我使用隐式意图从通知中启动应用程序,所以我需要指定一个类别。如果我没有通知的隐式Pendingintent什么都不做(因为它找不到对应的Activity)
  • @ol_v_er 你解决了这个问题吗?谢谢

标签: android android-intent android-notifications


【解决方案1】:

将操作android.intent.action.MAIN 和类别android.intent.category.LAUNCHER 添加到您的活动规范中。拥有两个动作和类别似乎是个 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中有描述:

这种意图过滤器会为 要在应用程序启动器中显示的活动,为用户提供 启动 Activity 并返回到它创建的任务的方式 启动后的任何时间。

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

【讨论】:

  • 我已将新操作和新类别添加到清单中 => 没有变化。我还尝试使用以下数据更改 Intent 创建,而不是更好:notificationIntent.setAction(Intent.ACTION_MAIN); notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER); notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 这样它应该开始一项新任务,但它仍然没有。
  • 尝试将android:launchMode 设置为singleTop。我读过其他尝试过并使它起作用的人。如果不是 - 这可能是 Android 中的一个错误。
  • :-( 那一定是个bug。肯定能把它添加到最近的应用列表中...
  • 无论如何我都会奖励你的赏金。感谢您的帮助
  • 抱歉,我没能帮您弄清楚 - 非常感谢。
【解决方案2】:

尝试将以下内容添加到清单中:

android:excludeFromRecents = "false"

祝你好运, 路易斯

【讨论】:

  • 这是文档中指定的默认值:developer.android.com/guide/topics/manifest/…
  • @ol_v_er 我知道这是默认设置,但是正如您所说,从通知启动时应用程序不会在最近历史记录中注册事件,因此通知可能会将此标志添加到起始意图中值为 true,如果是这种情况,此测试将检查您是否可以覆盖它。祝你好运
【解决方案3】:

您需要为每个操作/类别对使用单独的Intent Filters

<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>
    <intent-filter>
        <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:label="@string/app_name"吗?
【解决方案4】:

我遇到了同样的问题。问题是我的活动有 android:label=""。当我从通知中打开活动时,它将应用程序任务中的根活动替换为当前活动。 Android 会从“最近”中排除带有空标签的活动(请参阅 source code)。

要隐藏活动标题,您应该使用:

getActionBar().setDisplayShowTitleEnabled(false);

【讨论】:

    【解决方案5】:

    老话题,但我发现了解决方案(我在网上找不到任何人),也许它会对某人有所帮助。它无法将任何活动设置为父级,但我认为(不确定)如果有必要,您可以通过编程方式(在 OpenFromNotificationActivity 中)更改它。

    在清单中

    <activity android:name=".OpenFromNotificationActivity" 
                android:label="@string/label">
                <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".StartSplashActivity" />
     </activity>
    
     <activity android:name=".StartSplashActivity"
                android:label="@string/app_name">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
     </activity>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-22
      • 2014-07-16
      • 2014-04-05
      • 1970-01-01
      相关资源
      最近更新 更多