什么是粘性广播? [英] What is a Sticky Broadcast?
问题描述
我在 android 文档中遇到了这个术语,并附有定义
<块引用>这些是广播,其数据在完成后由系统保存,以便客户端可以快速检索该数据,而无需等待下一次广播.
什么意思?有人可以用一个特定的例子详细说明它的用法吗?我相信我们必须请求许可才能使用此意图?为什么会这样?
- 允许应用程序广播粘性意图.
如果 Activity 调用 onPause
并使用正常广播,则可能会错过接收广播.在 onResume
中启动后可以检查粘性广播.
2020 年 6 月 23 日更新
不推荐使用粘性广播.
<块引用>此方法已在 API 级别 21 中弃用.
<块引用>
不应使用粘性广播.它们不提供安全性(任何人都可以访问它们)、没有保护(任何人都可以修改它们)以及许多其他问题.推荐的模式是使用非粘性广播来报告某些内容发生了变化,并使用另一种机制让应用在需要时检索当前值.
实施
Intent intent = new Intent(some.custom.action");intent.putExtra(some_boolean",真);发送StickyBroadcast(意图);
资源
见
removeStickyBroadcast(Intent)
,在 API Level 5 + 上,isInitialStickyBroadcast()
用于接收器的onReceive
.
I came across this term in the android documentation with the accompanying definition
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.
What does it mean? Can someone elaborate its use with a particular example? I believe we have to request a permission for using this intent? Why so?
<uses-permission android:name="android.permission.BROADCAST_STICKY"/> - Allows an application to broadcast sticky intents.
If an Activity calls onPause
with a normal broadcast, receiving the Broadcast can be missed. A sticky broadcast can be checked after it was initiated in onResume
.
Update 6/23/2020
Sticky broadcasts are deprecated.
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.
Implement
Intent intent = new Intent("some.custom.action");
intent.putExtra("some_boolean", true);
sendStickyBroadcast(intent);
Resources
Related post: What is the difference between sendStickyBroadcast and sendBroadcast in Android?
See
removeStickyBroadcast(Intent)
, and on API Level 5 +,isInitialStickyBroadcast()
for usage in the Receiver'sonReceive
.
这篇关于什么是粘性广播?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!