理解sendStickyBroadcast
抄录了关于sendStickyBoradcast()的相关理解.
关于sendStickyBoradcast(),SDK中的相关说明:
引用
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
一个小的知识点整理,希望大家有用.