Android 中的 sendStickyBroadcast 和 sendBroadcast 有什么区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2584497/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
what is the difference between sendStickyBroadcast and sendBroadcast in Android
提问by cobject
What is the difference between sendStickyBroadcast
and sendBroadcast
in Android?
Android 中sendStickyBroadcast
和之间有什么区别sendBroadcast
?
回答by CommonsWare
Here is what the Android SDK says about sendStickyBroadcast()
:
以下是 Android SDK 所说的内容sendStickyBroadcast()
:
Perform a sendBroadcast(Intent) that is "sticky," meaning the Intent you are sending stays around after the broadcast is complete, so that others can quickly retrieve that data through the return value of registerReceiver(BroadcastReceiver, IntentFilter). In all other ways, this behaves the same as sendBroadcast(Intent).
执行“粘性”的 sendBroadcast(Intent),这意味着您发送的 Intent 在广播完成后仍然存在,以便其他人可以通过 registerReceiver(BroadcastReceiver, IntentFilter) 的返回值快速检索该数据。在所有其他方面,这与 sendBroadcast(Intent) 的行为相同。
One example of a sticky broadcast sent via the operating system is ACTION_BATTERY_CHANGED
. When you call registerReceiver()
for that action -- even with a null
BroadcastReceiver
-- you get the Intent
that was last broadcast for that action. Hence, you can use this to find the state of the battery without necessarily registering for all future state changes in the battery.
通过操作系统发送的粘性广播的一个例子是ACTION_BATTERY_CHANGED
. 当您调用registerReceiver()
该操作时——即使使用了null
BroadcastReceiver
——您将获得该Intent
操作的最后一次广播。因此,您可以使用它来查找电池的状态,而不必注册电池中所有未来的状态变化。
回答by Umang Kothari
Types:- Local,Normal,Ordered and Sticky
类型:- 本地、正常、有序和粘性
Normal Broadcast
普通广播
:- use sendBroadcast()
:- 使用 sendBroadcast()
:- asynchronous broadcast
:- 异步广播
:- any receiver receives broadcast not any particular order
:- 任何接收器都接收广播而不是任何特定顺序
Ordered Broadcast
有序广播
:- use sendOrderedBroadcast()
:- 使用 sendOrderedBroadcast()
:- synchronous broadcast
:- 同步广播
:- receiver receives broadcast in priority base
:- 接收器接收优先级广播
:- we can also simply abort broadcast in this type
:- 我们也可以简单地中止这种类型的广播
Local Broadcast
本地广播
:- use only when broadcast is used only inside same process
:- 仅当广播仅在同一进程内使用时使用
Sticky Broadcast
粘性广播
:- normal broadcast intent is not available any more after this was send and processed by the system.
:- 普通的广播意图在系统发送和处理后不再可用。
:- use sendStickyBroadcast(Intent)
:- 使用 sendStickyBroadcast(Intent)
:- the corresponding intent is sticky, meaning the intent you are sending stays around after the broadcast is complete.
:- 相应的意图是粘性的,这意味着您发送的意图在广播完成后仍然存在。
:- because of this others can quickly retrieve that data through the return value of registerReceiver(BroadcastReceiver, IntentFilter).
:- 因此,其他人可以通过 registerReceiver(BroadcastReceiver, IntentFilter) 的返回值快速检索该数据。
:- apart from this same as sendBroadcast(Intent).
:- 除了这与 sendBroadcast(Intent) 相同。
回答by Rajesh
sendbroadcast()
- normal broadcast, but we can set priority as well.
sendbroadcast()
- 正常广播,但我们也可以设置优先级。
sendstickybroadcast()
- intent passed with this will be stick for future users who are registering through code (dynamic receivers).
The broadcast that will stick with android, and will be re-delivered or re-broadcasted to the future requests from any broadcast receivers
sendstickybroadcast()
- 通过此传递的意图将适用于通过代码(动态接收器)注册的未来用户。将与 android 保持一致的广播,并将重新传送或重新广播到任何广播接收器的未来请求
When somebody sends a sticky broadcast using sendstickyBroadcast(intent);
then that broadcast will be available for the future users who are using dynamic receivers.
当有人使用sendstickyBroadcast(intent);
该广播发送粘性广播时,该广播将可供使用动态接收器的未来用户使用。
But Now you should not use sendStickyBroadcast()
method it is deprecated
但是现在你不应该使用 sendStickyBroadcast()
它已被弃用的方法
From Android 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
此方法在 API 级别 21 中已弃用。不应使用粘性广播。它们不提供安全性(任何人都可以访问它们)、没有保护(任何人都可以修改它们)以及许多其他问题。推荐的模式是使用非粘性广播来报告某些事情发生了变化,以及应用程序在需要时检索当前值的另一种机制
I hope this helps.
我希望这有帮助。