Android通知启动相同的活动两次

发布于 2024-10-05 19:07:50 字数 685 浏览 1 评论 0原文

当我单击状态栏上的通知时,它会启动一个活动,但行为很奇怪。如果我的应用程序位于前台并且我单击通知,则通知意图将被触发一次。如果我的应用程序在后台运行,则通知意图会被触发两次。如果我退出应用程序(即通过点击后退按钮弹出所有活动),则通知意图将被触发一次。任何人都可以解释这种行为。代码片段如下:

_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌吟 2024-10-12 19:07:50
_showActivityIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

试试这个。它将阻止同一活动的多个实例。你也可以把它放在清单中

_showActivityIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

Try this. it will prevent multiple instances of the same activity. u can put this in the manifest also

小矜持 2024-10-12 19:07:50

所有标志均不起作用:

  Intent().apply {
            addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
            addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)
            addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            ...

您需要在该活动的清单中添加:

android:launchMode="singleTask"
android:taskAffinity=""
android:excludeFromRecents="true"

检查: https://developer.android.com/develop/ui/views/notifications/navigation

None of the flags worked:

  Intent().apply {
            addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
            addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)
            addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            ...

You need to add in the Manifest for that activity:

android:launchMode="singleTask"
android:taskAffinity=""
android:excludeFromRecents="true"

Check: https://developer.android.com/develop/ui/views/notifications/navigation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文