在用于通知的未决意图中使用 FLAG_ONE_SHOT 的含义是什么?有必要吗?

发布于 2025-01-09 18:25:38 字数 495 浏览 3 评论 0原文

Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);

NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, channelId)
                        .setContentIntent(pendingIntent);

是不是意味着我不能通过重复点击这个通知来多次跳转到mainActivity?当我单击通知时,pendingIntent.send() 会在内部调用吗?该标志在通知中是否变得毫无意义?

Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);

NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, channelId)
                        .setContentIntent(pendingIntent);

Does it mean that I can't jump to the mainActivity multiple times by repeatedly clicking this notification? Is pendingIntent.send() called internally when I clicks the notification? Does this flag become meaningless in the notifications?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

是你 2025-01-16 18:25:38

PendingIntent.FLAG_ONE_SHOT表示PendingIntent使用一次后将被删除。通常,如果系统中存在其他对 PendingIntent 的引用,则 PendingIntent 可能会挂起。

该标志可用于防止多次使用 PendingIntent,尽管我个人的经验是该标志通常会导致比它解决的问题更多的问题。

你问:

当我点击通知时,pendingIntent.send() 会在内部调用吗?

是的,这基本上就是当您单击与 Notification 中的 PendingIntent 关联的 UI 元素时发生的情况。

PendingIntent.FLAG_ONE_SHOT means that the PendingIntent will be deleted after it is used once. Normally, a PendingIntent can hang around if there are other references to it in the system.

This flag can be used to prevent multiple uses of the PendingIntent, although my personal experience is that this flag generally causes more problems than it solves.

You asked:

Is pendingIntent.send() called internally when I clicks the notification?

Yes, that's basically what happens when you click on the UI element associated with the PendingIntent in the Notification.

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