【发布时间】:2013-06-02 18:06:55
【问题描述】:
我正在使用这个解决方案:How to make notification intent resume rather than making a new intent?
当我正常运行我的应用程序时,我工作正常。但是我的应用程序具有共享功能。
当我从图库应用程序中选择我想要分享的图像并在我的活动中打开它时,我会在活动中创建一个通知。我希望当用户点击通知时,它会打开现有活动(由图库应用打开)
问题是,当我点击通知时,它不会恢复该活动,而是打开该活动的新实例或之前已打开的另一个实例
编辑: 更多解释
我的应用名为 ShareApp,活动名为 ShareActivity 问题是当我通过图库应用程序打开 ShareActivity 时,会在图库应用程序任务堆栈的顶部创建一个 ShareActivity 实例。现在,当我创建一个指向我的 ShareActivity 的通知时,我使用了意图:
Intent intent = new Intent(this, ShareActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
问题是当我点击通知时,它指向 ShareApp 任务堆栈中的 ShareActivity 实例,而不是图库应用程序任务的堆栈..
知道如何指向正确的任务堆栈吗?
编辑 2:我的代码
int defaults = Notification.FLAG_NO_CLEAR;
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(getString(R.string.app_name))
.setContentText(text)
.setDefaults(defaults);
Intent intent = new Intent(this, this.getClass());
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
mBuilder.setContentIntent(pendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(_ID, mBuilder.build());
编辑 3:adb shell dumpsys 活动(在应用 David https://stackoverflow.com/a/16901603/1334268 的代码后)
我的应用名为 shareApp,我的活动是 ShareActivity
点击通知前:
Main stack:
TaskRecord{41965708 #6 A com.android.gallery3d}
Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.android.gallery3d/.app.Gallery bnds=[364,50][108,108]}
Hist #2: ActivityRecord{417ccc28 com.yeahman.shareapp/.ShareActivity}
Intent { act=android.intent.action.SEND_MULTIPLE typ=image/* cmp=com.yeahman.shareapp/.ShareActivity (has extras) }
ProcessRecord{41c868d0 6088:com.yeahman.shareapp/10116}
Hist #1: ActivityRecord{4135e540 com.android.gallery3d/.app.Gallery}
Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.android.gallery3d/.app.Gallery bnds=[364,50][108,108] }
点击通知后:
Main stack:
TaskRecord{41965708 #6 A com.android.gallery3d}
Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.android.gallery3d/.app.Gallery bnds=[364,50][108,108]}
Hist #3: ActivityRecord{4169f358 com.android.gallery3d/.app.Gallery}
Intent { flg=0x20000000 cmp=com.android.gallery3d/.app.Gallery bnds=[0,205][480,301] }
ProcessRecord{4175dd28 5808:com.android.gallery3d/10036}
Hist #2: ActivityRecord{417ccc28 com.yeahman.shareapp/.ShareActivity}
Intent { act=android.intent.action.SEND_MULTIPLE typ=image/* cmp=com.yeahman.shareapp/.ShareActivity (has extras) }
ProcessRecord{41c868d0 6088:com.yeahman.shareapp/10116}
Hist #1: ActivityRecord{4135e540 com.android.gallery3d/.app.Gallery}
Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.android.gallery3d/.app.Gallery bnds=[364,50][108,108] }
【问题讨论】:
-
发布您创建
Intent、PendingIntent和Notification的代码。 -
我刚刚用我的代码编辑了我的问题
-
有解决办法吗?我会接受这是不可能的,作为带有备用解释的答案
-
在阅读教程/文档之后的帖子和教程/文档之后......我得出的结论是,不幸的是这是不可能的:(
标签: android notifications