Thursday, July 24, 2014

Android: What Is Sticky Broadcast?



Broadcasts

Broadcasts are announcements sent to Android apps. When the battery gets low or the screen turns off, for example, an announcement goes out to all applications. Your phone's operating system initiates most broadcasts, but individual apps may do it too. If an app receives data that's available to other applications, it will contact and inform them. For the announcement to reach the apps, it has to go first to a broadcast receiver, an application component that handles the messages, usually without notifying users what it's doing.

Intents

Because of the separation the Android imposes between its various apps, one app cannot directly contact another app's broadcast receiver. Instead, the app notifies the Android phone with an Intent. An Intent is a message to either activate an app or activate one of its components, such as the receiver. The Intent simply defines the message that's about to be broadcast – that the battery is low, for example, or to request access to a photograph on your phone.

Sticky Broadcasts

A normal broadcast reaches the receiver it's intended for, then terminates. A sticky broadcast remains in existence so that it can notify other apps if they need the same information – for example, that the battery is now fully charged. When you register a new app that needs to know the information, or an inactive app turns on, the sticky broadcast will communicate with the receiver. A later sticky broadcast with new information on the same topic will rewrite an earlier sticky broadcast.

Considerations

An app can only send out a sticky broadcast or remove one if its programming includes a permission authorizing stickies. The security on sticky broadcasts isn't as tight as on non-sticky messages. One of the good things about using an intent, sticky or not, is that while you can specify which apps you want to receive it, you don't have to. Your Android determines the right apps to receive your broadcast, based on the filtering built into each app by its programmer.

No comments:

Post a Comment