我有一个带有通知的应用程序,如果我点击它们就会打开某个活动.我想要的是,如果我点击通知并且活动已经打开,它就不会再次启动,而是会被带到前面.
我以为我可以用国旗做FLAG_ACTIVITY_BROUGHT_TO_FRONT或FLAG_ACTIVITY_REORDER_TO_FRONT,但它一直再次打开它,所以我有两次活动.
这是我的代码:
event_notification = new Notification(R.drawable.icon,
mContext.getString(R.string.event_notif_message), System.currentTimeMillis());
Intent notificationIntent = new Intent(mContext, EventListActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
sendNotification(event_notification, notificationIntent, mContext.getString(R.string.event_notif_title),
body, Utils.PA_NOTIFICATIONS_ID);
Run Code Online (Sandbox Code Playgroud)
我可以使用标志管理它,还是应该在SharedPreferences中存储变量以检查它是否已打开?
谢谢!
从Android开发,我通过示例代码实现一个简单的通知,但在我的应用程序中,我不想新的意图并再次创建活动,我只想回到我的上一个活动(它是一个媒体播放器UI).如果我使用示例代码,它将创建一个新的活动
Intent resultIntent = new Intent(this, ResultActivity.class);
Run Code Online (Sandbox Code Playgroud)
我评论有关新意图的相关代码并收到通知,但不知道如何回到我上次的活动......
从长按主页键回到我的应用程序并触摸我的应用程序是好的.我想要的就像这种行为.
bleow是Android开发的示例代码,我评论新的意图部分.
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!");
/*Intent resultIntent = new Intent(this, ResultActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(ResultActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);*/
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(mId, mBuilder.build());
Run Code Online (Sandbox Code Playgroud)
我认为我需要为活动的意图和适当性设置标志.因此,对于我想要回到我设定的活动
android:launchMode="singleInstance"
Run Code Online (Sandbox Code Playgroud)
因为,我不想从意图开始新的活动,我只想要我的最后一个活动,所以我补充说
Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY
Run Code Online (Sandbox Code Playgroud)
从文档我得到了使用Pendingintent.contentIntent它必须包含FLAG_ACTIVITY_NEW_TASK标志,并在使用该标志,如果一个任务已经为你现在开始的活动运行,则新的活动将不会启动; 相反,当前的任务将简单地带到它最后一个状态的屏幕前面.所以我也添加
Intent.FLAG_ACTIVITY_NEW_TASK
Run Code Online (Sandbox Code Playgroud)
但是从logCat,我仍然看到当我触摸返回活动的通知时,pendingintent仍然调用onCreate()函数而不是仅显示仍然"活着"的最后一个活动.
我正在使用这个解决方案:如何使通知意图恢复而不是制定新的意图?
当我正常运行我的应用程序时工作正常..但我的应用程序有共享功能.
当我选择要从图库应用程序共享的图像并在我的活动中打开它时,我在活动中创建通知.我希望当用户点击通知时,它会打开现有活动(由图库应用打开)
问题是,当我点击通知时,它不会恢复该活动,而是打开活动的新实例或之前已打开的另一个实例
编辑:更多解释
我的应用程序名为ShareApp,活动名为ShareActivity,问题是当我通过图库应用程序打开ShareActivity时,会在图库应用程序任务的堆栈顶部创建一个ShareActivity实例.现在,当我创建指向我的ShareActivity的通知时,我使用了intent:
Intent intent = new Intent(this, ShareActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Run Code Online (Sandbox Code Playgroud)
问题是,当我点击通知时,它指向ShareApp任务的堆栈中的ShareActivity实例,而不是gallery app任务的堆栈.
任何想法如何指向正确的任务的堆栈?
编辑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());
Run Code Online (Sandbox Code Playgroud)
编辑3:adb shell dumpsys活动(在应用David代码之后/sf/answers/1183112241/)
我的应用程序名为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 …Run Code Online (Sandbox Code Playgroud)