Saturday 6 May 2017

Sticky Broadcast


Sticky Broadcast(s) are a special kind of Broadcast(s) that remain active for awhile after being sent. You can send a sticky Broadcast with the method:
Context.sendStickyBroadcast()

If you register a BroadcastReceiver that listens to sticky Broadcast(s), it can receive two kind of Broadcast(s):
  1. Broadcast(s) that have just been sent
  2. Broadcast(s) that have been sent previously and are still active
You can find out what kind of Broadcast you have just received with the method:
isInitialStickyBroadcast()
returning false if the Broadcast represents an event that has just occured.

Sticky Broadcast(s) are generally used for events lasting for a certain period of time. Examples of such Broadcast(s) sent by the system are:
  • Intent.ACTION_BATTERY_CHANGED: indicates changes in the battery level. BroadcastReceiver(s) must be programmatically registered with Context.registerReceiver()
  • Intent.ACTION_DOCK_EVENT: indicates when a device is placed on a dock
  • Intent.ACTION_DEVICE_STORAGE_LOW: indicates low memory condition
public class MyBroadcastReceiver extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if(action.equals(Intent.ACTION_DOCK_EVENT)) {
            if(!isInitialStickyBroadcast()) {
                // this is a new broadcast
            } 
            else {
                // this is an old broadcast
            }
        }
    }
}

In order to send sticky Broadcast(s) you must include the permission android.permission.BROADCAST_STICKY in the Manifest file.

If you want to remove a sticky Broadcast you can use the method:
Context.removeStickyBroadcast(Intent intent)
passing the Intent that was previously broadcast.

NB. Sticky Broadcast(s) consume system resources, so use them sparingly when really needed!

API level 21

Starting from API level 21 sticky Broadcast(s) are deprecated.
"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".

7 comments:

  1. I constantly like to read a top quality content having accurate info pertaining to the subject and the exact same thing I found in this article. Nice job.
    Bulk faxing

    ReplyDelete
  2. Here in this blog, words themselves are creating that scenario where there is a lot of mayhem and serendipity along with a silent isolation. Rarely found to see such skill and enthusiasm.Fax broadcasting Effective, fast, affordable and Bulk faxing

    ReplyDelete
  3. Great post. I was checking continuously this blog and I am impressed! Extremely useful information specially the last part :) I care for such information much. I was seeking this particular info for a long time. Thank you and good luck.mass faxing

    ReplyDelete
  4. My partner and I absolutely love your blog and find most of your post's to be just what I'm looking for. Would you offer guest writers to write content available for you? I wouldn't mind publishing a post or elaborating on some of the subjects you write about here. Again, awesome site!fax list

    ReplyDelete
  5. I was curious if you ever thought of changing the structure of your site? Its very well written; I love what youve got to say. But maybe you could a little more in the way of content so people could connect with it better. Youve got an awful lot of text for only having 1 or two pictures. Maybe you could space it out better? fax broadcasting

    ReplyDelete
  6. Positive site, where did u come up with the information with this posting? I'm pleased I came across it though, ill be checking back soon to learn what additional posts you include.fax list

    ReplyDelete
  7. I discovered your this post while trying to find details about blog-related research ... It is a good post .. keep posting and updating information.fax list

    ReplyDelete