簡體   English   中英

從通知中恢復活動

[英]resuming an activity from a notification

我的應用狀態欄中有通知:

    Notification notification = new Notification(R.drawable.icon, null, System.currentTimeMillis());

    Intent notificationIntent = new Intent(this.parent, MainActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this.parent, 0, notificationIntent, 0);

    ...

    notification.flags = Notification.FLAG_ONGOING_EVENT;        
    mNotificationManager.notify(NOTIFICATION_ID, notification);

這個問題是當你從應用程序按下主頁按鈕(將其推到后台),然后按下從狀態欄訪問的列表中的通知,它會啟動活動的新副本。 我想要做的就是恢復應用程序(比如當你長按主頁按鈕並按下應用程序的圖標時)。 有沒有辦法創建一個Intent來做到這一點?

我通過改變解決了這個問題launchMode我的活動,以singleTask在AndroidManifest.xml文件。

此屬性的默認值為standard ,允許運行任意數量的實例。

“singleTask”和“singleInstance”活動只能開始一項任務。 它們始終位於活動堆棧的根部。 此外,設備一次只能保存一個活動實例 - 只有一個這樣的任務。 [...]

“singleTask”和“singleInstance”模式在一個方面也各不相同:“singleTask”活動允許其他活動成為其任務的一部分。 它始終是其任務的根源,但其他活動(必然是“標准”和“單一活動”)可以啟動到該任務中。 另一方面,“singleInstance”活動不允許其他活動成為其任務的一部分。 這是任務中唯一的活動。 如果它啟動另一個活動,則該活動將分配給另一個任務 - 就像FLAG_ACTIVITY_NEW_TASK在意圖中一樣。

您可以在Android開發人員指南中找到詳細說明

我希望這有幫助

我遇到了類似的問題,處理這個問題的正確方法是使用標志:Intent.FLAG_ACTIVITY_CLEAR_TOP和Intent.FLAG_ACTIVITY_SINGLE_TOP之類的

notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

從文檔中,這將:

FLAG_ACTIVITY_CLEAR_TOP

如果已設置,並且正在啟動的活動已在當前任務中運行,則不會啟動該活動的新實例,而是將關閉其上的所有其他活動,並將此Intent傳遞給(現在開啟) top)舊活動作為新的意圖。

FLAG_ACTIVITY_SINGLE_TOP

如果設置,則如果活動已在歷史堆棧的頂部運行,則不會啟動該活動。

我剛剛找到了解決這個問題的方法:我創建了getPreviousIntent方法並將其提供給PendingIntent,這就是全部:

private Intent getPreviousIntent(Intent newIntent) {
    final ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    final List<ActivityManager.RecentTaskInfo> recentTaskInfos = am.getRecentTasks(1024,0);
    String myPkgNm = getPackageName();

    if (!recentTaskInfos.isEmpty()) {
        ActivityManager.RecentTaskInfo recentTaskInfo;
        for (int i = 0; i < recentTaskInfos.size(); i++) {
            recentTaskInfo = recentTaskInfos.get(i);
            if (recentTaskInfo.baseIntent.getComponent().getPackageName().equals(myPkgNm)) {
                newIntent = recentTaskInfo.baseIntent;
                newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            }
        }
    }
    return newIntent;
}

將此行添加到應用清單文件中的相應活動。

android:launchMode="singleTask"

例如:

<activity
    android:name=".Main_Activity"
    android:label="@string/title_main_activity"
    android:theme="@style/AppTheme.NoActionBar"
    android:launchMode="singleTask" />

我在設備之間存在不一致的行為,其中一些意圖丟失額外的東西,MainActivity的OnNewIntentonCreate方法都被調用。

什么行不通

表現:

    <activity android:name=".MainActivity"
        android:launchMode="singleTask">

服務:

    Intent notificationIntent = new Intent(this.getBaseContext(), MainActivity.class);
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    notificationIntent.putExtra("TEST", 1);

做了什么工作

表現:

    <activity android:name=".MainActivity"
        android:launchMode="singleTask">

服務:

    PackageManager pm = getPackageManager();
    Intent launchIntent = m.getLaunchIntentForPackage(BuildConfig.APPLICATION_ID);
    launchIntent.putExtra("TEST", 2);

我用:

notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
notificationIntent.setAction(Intent.ACTION_MAIN);

不確定這些是您需要設置的值,但答案是在那些方法/標志中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM