Android 什么是粘性广播?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3490913/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:26:43  来源:igfitidea点击:

What is a Sticky Broadcast?

androidandroid-intent

提问by Shouvik

I came across this term in the android documentation with the accompanying definition

我在带有随附定义的 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.

这些是广播,其数据在完成后由系统保存,以便客户端可以快速检索该数据,而无需等待下一次广播。

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.

回答by Paul Burke

Please read Mark Murphy's explanation here: what is the difference between sendStickyBroadcast and sendBroadcast in Android

请在此处阅读 Mark Murphy 的解释: Android 中的 sendStickyBroadcast 和 sendBroadcast 有什么区别

Here's an abstract example of how one might use a sticky broadcast:

这是一个如何使用粘性广播的抽象示例:

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

If you are listening for this broadcast in an Activity that was frozen (onPause), you could miss the actual event. This allows you to check the broadcast after it was fired (onResume).

如果您在已冻结 (onPause) 的 Activity 中收听此广播,您可能会错过实际事件。这允许您在广播被触发后检查广播 (onResume)。

EDIT:More on sticky broadcasts...

编辑:更多关于粘性广播...

Also check out removeStickyBroadcast(Intent), and on API Level 5 +, isInitialStickyBroadcast()for usage in the Receiver's onReceive.

另请查看removeStickyBroadcast(Intent)和 API 级别 5 +, isInitialStickyBroadcast()以了解在 Receiver 的onReceive.

Hope that helps.

希望有帮助。

回答by Narendra Motwani

sendStickyBroadcast()performs a sendBroadcast(Intent)known as sticky, i.e. 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). 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.

sendStickyBroadcast()执行一个sendBroadcast(Intent)称为粘性的,即您发送的 Intent 在广播完成后保留,以便其他人可以通过 的返回值快速检索该数据registerReceiver(BroadcastReceiver, IntentFilter)。在所有其他方面,这与sendBroadcast(Intent). 通过操作系统发送的粘性广播的一个例子是ACTION_BATTERY_CHANGED. 当你调用registerReceiver()那个动作时——即使是空值BroadcastReceiver——你会得到最后一次为该动作广播的 Intent。因此,您可以使用它来查找电池的状态,而不必注册电池中所有未来的状态变化。

回答by Lou Morda

The value of a sticky broadcast is the value that was last broadcast and is currently held in the sticky cache. This is not the value of a broadcast that was received right now. I suppose you can say it is like a browser cookie that you can access at any time. The sticky broadcast is now deprecated, per the docs for sticky broadcast methods (e.g.):

粘性广播的值是上次广播且当前保存在粘性缓存中的值。这不是现在收到的广播的值。我想你可以说它就像一个你可以随时访问的浏览器 cookie。根据粘性广播方法的文档(例如),粘性广播现在已被弃用:

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 中已弃用。不应使用粘性广播。它们不提供安全性(任何人都可以访问它们)、没有保护(任何人都可以修改它们)以及许多其他问题。推荐的模式是使用非粘性广播来报告某些内容已更改,并使用另一种机制让应用程序在需要时检索当前值。

回答by Arul Pandian

A normal broadcast Intent is not available anymore after is was send and processed by the system. If you use the sendStickyBroadcast(Intent) method, the Intent is sticky, meaning the Intent you are sending stays around after the broadcast is complete.

普通的广播 Intent 被系统发送和处理后不再可用。如果您使用 sendStickyBroadcast(Intent) 方法,则 Intent 是粘性的,这意味着您发送的 Intent 在广播完成后仍然存在。

you refer to my blog:enter link description here

你参考我的博客:在此处输入链接描述