Local Broadcast Manager in Android

The Local Broadcast Manager was introduced to the Android Support Library to simplify the process of registering for, and sending, Broadcast Intents between components within your application

Because of the reduced broadcast scope, using the Local Broadcast Manager is more efficient than sending a global broadcast. It also ensures that the Intent you broadcast cannot be received by any components outside your application, ensuring that there is no risk of leaking private or sensitive data, such as location information.

Similarly, other applications can’t transmit broadcasts to your Receivers, negating the risk of these Receivers becoming vectors for security exploits.

LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);

To register a local broadcast Receiver, use the Local Broadcast Manager’s registerReceiver method, much as you would register a global receiver, passing in a Broadcast Receiver and an Intent Filter:

lbm.registerReceiver(new BroadcastReceiver() {
 public void onReceive(Context context, Intent intent) {
 // TODO Handle the received local broadcast
 }, new IntentFilter(LOCAL_ACTION));

Note that the Broadcast Receiver specified can also be used to handle global Intent broadcasts

To transmit a local Broadcast Intent, use the Local Broadcast Manager’s sendBroadcast method, passing in the Intent to broadcast:

lbm.sendBroadcast(new Intent(LOCAL_ACTION));

The Local Broadcast Manager also includes a sendBroadcastSync method that operates synchronously, blocking until each registered Receiver has been dispatched.

Leave a Comment