所以我目前正在显示通知.当用户单击此注释时,将启动该应用程序.通知仍然存在,表示该服务正在后台运行.
Intent notificationIntent = new Intent(context, LaunchActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(1, notification);
Run Code Online (Sandbox Code Playgroud)
但是,我发现了一个出现错误的情况.如果用户通过单击正常图标启动应用程序,并且在活动正在运行时单击通知,则会启动新活动,而不会退出先前的活动,后者位于较早的活动之上.这还不是全部:进一步点击通知将创建其他活动并将其置于已经运行的活动之上.我怎么能阻止这个?是否有一个很好的检查,以查看某个活动当前是否正在显示或已加载?
当我单击状态栏上的通知时,它会启动一个活动,但行为很奇怪.如果我的应用程序在前台,我单击通知,通知意图将被触发一次.如果我的应用程序在后台,那么通知意图会被触发两次.如果我退出应用程序(即通过点击后退按钮弹出所有活动),则会触发通知意图一次.谁能解释这种行为.代码段如下:
_notification = new Notification(icon_id, "Ticker Text", System.currentTimeMillis());
_showActivityIntent = new Intent();
_showActivityIntent.setAction(MyActivityName);
_showActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK + Intent.FLAG_ACTIVITY_NO_HISTORY);
_showActivityPendingIntent = PendingIntent.getActivity(context, 0, _showActivityIntent, 0);
_notification.setLatestEventInfo(context, "My title", "My text", _showActivityPendingIntent);
_notificationMgr.notify(notificationId, _notification);
Run Code Online (Sandbox Code Playgroud)