Android通知启动相同的活动两次
当我单击状态栏上的通知时,它会启动一个活动,但行为很奇怪。如果我的应用程序位于前台并且我单击通知,则通知意图将被触发一次。如果我的应用程序在后台运行,则通知意图会被触发两次。如果我退出应用程序(即通过点击后退按钮弹出所有活动),则通知意图将被触发一次。任何人都可以解释这种行为。代码片段如下:
_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);
When I click the notification on the status bar it launches an activity but the behavior is strange. If my app is in foreground and I click the notification the notification intent is fired once. If my app is in background then the notification intent is fired twice. If I exit the app (ie all activities have been popped by hitting the back button) then notification intent is fired once. Can anyone explain this behaviour. The code snippet is as follows:
_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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个。它将阻止同一活动的多个实例。你也可以把它放在清单中
Try this. it will prevent multiple instances of the same activity. u can put this in the manifest also
所有标志均不起作用:
您需要在该活动的清单中添加:
检查: https://developer.android.com/develop/ui/views/notifications/navigation
None of the flags worked:
You need to add in the Manifest for that activity:
Check: https://developer.android.com/develop/ui/views/notifications/navigation