gpt4 book ai didi

android - 什么是粘性广播?

转载 作者:IT老高 更新时间:2023-10-28 13:08:43 25 4
gpt4 key购买 nike

我在附带定义的 android 文档中遇到了这个术语

These are broadcasts whose data is held by the system after being finished, so that clients can quickly retrieve that data without having to wait for the next broadcast.

这是什么意思?有人可以用一个特定的例子详细说明它的用途吗?我相信我们必须请求许可才能使用此 Intent ?为什么会这样?

<uses-permission android:name="android.permission.BROADCAST_STICKY"/> - Allows an application to broadcast sticky intents.

最佳答案

如果 Activity 使用普通广播调用 onPause,则可能会错过接收广播。在 onResume 中启动后,可以检查粘性广播。

2020 年 6 月 23 日更新

不推荐使用粘性广播。

See sendStickyBroadcast documentation.

This method was deprecated in API level 21.

Sticky broadcasts should not be used. They provide no security (anyone can access them), no protection (anyone can modify them), and many other problems. The recommended pattern is to use a non-sticky broadcast to report that something has changed, with another mechanism for apps to retrieve the current value whenever desired.

实现

Intent intent = new Intent("some.custom.action");
intent.putExtra("some_boolean", true);
sendStickyBroadcast(intent);

资源

关于android - 什么是粘性广播?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3490913/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com