Introduction to Local Broadcast Manager in Android

Today we will learn about local broadcast manager in android. The Local Broadcast Manager was introduced to the Android Support Library to simplify the process of registering for, sending, and receiving Intents broadcast between components within your android applications. Using the Local Broadcast Manager is more efficient than sending a global broadcast. Local Broadcast Manager ensures that the Intent you broadcast cannot be received by any other applications, ensuring that there is no risk of leaking sensitive data.

Similarly, other applications can’t transmit broadcasts to your Receivers, negating the risk of these Receivers becoming vectors for security exploits. Note that the Broadcast Receiver specified can also be used to handle global Intent broadcasts. You need to include the Android Support Library in your application to use Local Broadcast Manager.

Use the LocalBroadcastManager.getInstance method to return an instance of the Local Broadcast Manager:

LocalBroadcastManager localBroadcastManager = 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 as shown

@Override
public void onResume() {
  super.onResume();

 // Registering the Broadcast Receiver
 LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
 lbm.registerReceiver(receiver, filter);
}

@Override  
public void onPause() {
 // Unregister the Broadcast receiver
 LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
 lbm.unregisterReceiver(receiver);
  super.onPause();
} 

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

localBroadcastManager.sendBroadcast(new Intent(LOCAL_ACTION));

Public Methods of Local Broadcast Manager

1- registerReceiver(BroadcastReceiver receiver, IntentFilter filter)

Register a receive for any local broadcasts that match the given IntentFilter.

2- getInstance(Context context)

3-sendBroadcast(Intent intent)

Broadcast the given intent to all interested BroadcastReceivers.

4-sendBroadcastSync(Intent intent)

similar to sendBroadcast(Intent), but if there are any receivers for the Intent this function will block and immediately dispatch them before returning.

5-unregisterReceiver(BroadcastReceiver receiver)

Unregister a previously registered BroadcastReceiver.

In this article we’ve learned the basics of Local Broadcast Manager in Android. So if you want some kind of broadcasting in your application then you should definitely use the LocalBroadcastManager and we should avoid using the Global Broadcast wherever we can because for using Global Broadcast you have to ensure that there are no security holes that can leak your sensitive or private data to other applications. Happy Coding..!

Leave a Reply

Your email address will not be published. Required fields are marked *

Trending