`
pJun
  • 浏览: 4072 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

理解sendStickyBroadcast

 
阅读更多
抄录了关于sendStickyBoradcast()的相关理解.

关于sendStickyBoradcast(),SDK中的相关说明:
引用
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).

引用

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.


上面的解释,不是特别好理解, 请看下面的解释.
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.
Heres 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).

EDIT: More on sticky boradcasts...

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


资料来源:
http://stackoverflow.com/questions/3490913/what-is-a-sticky-intent-android
http://stackoverflow.com/questions/2584497/what-is-the-difference-between-sendstickybroadcast-and-sendbroadcast-in-android

一个小的知识点整理,希望大家有用.
分享到:
评论

相关推荐

    sendStickyBroadcast-receiver

    sendStickyBroadcast接收方,其中有端流量监控代码 用的MainActivity.java

    sendStickyBroadcast

    发送方,sendStickyBroadcast

    最经典的sendBroadcast与sendStickyBroadcast的区别分析与讲解

    最经典的关于sendBroadcast与sendStickyBroadcast的区别分析与讲解

    sendBroadcast和sendStickyBroadcast的区别

    NULL 博文链接:https://aijiawang-126-com.iteye.com/blog/1021357

    android broadcast实例

    android broadcast实例,包括sendBroadcast、 sendOrderedBroadcast、 sendStickyBroadcast三种实现方法

    Android Intent和Intent Filter详解

     将intent对象传给 Context.sendBroadcast(), Context.sendOrderedBroadcast(),或者Context.sendStickyBroadcast()等广播方法,则它们被传给 broadcast receiver.  在上述三种情况下, android系统会自己找到合适的...

    Android开发指南

    • 传递给任意广播方法(例如Context.sendBroadcast(),Context.sendOrderedBroadcast(), 或者Context.sendStickyBroadcast())的意图对象被传递给所有感兴趣的广播接收者。许多种广播产生于系统代码。 在每个例子里...

    Android实验六.doc

    8) 在 AndroidManifest.xml 中声明 sendStickyBroadcast()对应的权限, 在字段之后定义。 实验结果截图: 图表 1 普通广播静态注册 图表 2 普通广播动态注册 图表 3 sticky广播动态注册 三、实验分析与小结: ...

    Android中BroadcastReceiver(异步接收广播Intent)的使用

    用于异步接收广播Intent,广播Intent的发送是通过调用Context.sendBroadcast()、广播接收者(BroadcastReceiver)用于异步接收广播Intent,广播...sendOrderedBroadcast()或者Context.sendStickyBroadcast()来实现的...

Global site tag (gtag.js) - Google Analytics