Android: What Is Sticky Broadcast?

By Fraser Sherman

A sticky broadcast is a tool Android developers use for communicating between apps. These broadcasts happen without the user being notified. The Android OS normally treats each application as if it were a separate user. Apps run independently and in isolation, on separate virtual machines, using only the hardware components they need in order to function. This leads to tight security, but sometimes apps need to share information, sticky broadcasts are one solution to getting information from one app to another.

Broadcasts

Broadcasts are announcements sent to Android apps. These happen without the user being aware of them. The Android operating system initiates most broadcasts, but individual apps can broadcast too. As an example, when the battery gets low or the screen turns off, an announcement goes out to all applications. If an app receives data that's available to other applications, it will inform them. For the announcement to reach the apps, it has to go first to a broadcast receiver, a component in the app that handles broadcast messages.

Android Intents

Because of the separation the Android imposes between its various apps, all broadcasts go through the operating system. An app cannot directly contact another app's broadcast receiver. When an app does want to send a broadcast, it notifies the operating system 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 sticks around 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 when an inactive app is launched, the sticky broadcast will be sent to the new app's receiver. A new sticky broadcast with updated 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. Any app can potentially overwrite another app's sticky. 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.