LocalBroadcastManager In Android

LocalBroadcastManager allows us to register for and send broadcast of intents to local objects within an application or process.

What is a Broadcast Receiver?

Broadcast Receiver is a component of Android, that allows our application to send and receive a broadcast from the Android system as well as other applications. There are many system-level events for which our app can get notified; for example, when the system boots up, when the device is connected with a Bluetooth device, when the device starts charging etc. Our application can also notify other applications or get notified by other applications using custom broadcast.

Whenever the event occurs for which we have registered broadcast receivers, Android runtime will notify the applications and we can perform any operation after that.

Ways to register broadcast receivers

There are two ways to register broadcast receivers, or we can say there are two types of broadcast receivers.

  • Manifest-declared (Static)
  • Context-registered (Dynamic)

If we declare a broadcast receiver in the manifest file, it will be live even when our application is not running. That means when a registered event occurs, our application will get notified and whatever operation we have performed in that, the event will get executed.

If we declare a broadcast receiver by a context (like in an Activity), our application can get notified as long as that activity is not destroyed. If we use the application context to register broadcast receiver, our application can get notified as long as an application is running.

NOTE
ANDROID (OREO, 8.0) HAS IMPOSED SOME RESTRICTION ON MANIFEST REGISTERED RECEIVERS. IF YOUR APPLICATION IS TARGETING ANDROID OREO (8.0), YOU CANNOT USE MANIFEST TO REGISTER A RECEIVER FOR BROADCAST THAT DOES NOT TARGET YOUR APPLICATION SPECIFICALLY.

Why do we need local broadcast if we have Broadcast Receiver?

Now, if we have broadcast receivers that can get notified by the system or any other application, what is the need of local broadcast? The answer is very simple. Using broadcast receiver can be risky because any application can intercept the broadcast for which our application is subscribed.

Any other Android application can send the same broadcast intent action for which we have registered a broadcast receiver, and any other Android application can listen to the broadcast of our application’s action. Below are the advantages of LocalBroadcastManager.

  • No data leakage; because it sends broadcast for objects within process or application.
  • Other application cannot send these broadcasts to our application.
Example
 
I have created a small demo application to show you the code.

MyReceiver

The code for Handling broadcast is shown here.

  1. public class MyReceiver extends BroadcastReceiver {  
  2.     public static final String ACTION_LOCAL_BROADCAST = "com.example.LOCAL";  
  3.     @Override  
  4.     public void onReceive(Context context, Intent intent) {  
  5.         // Handling receiver. Here we can put our logic to do the task.  
  6.         // We can check intent's action or data here  
  7.         if (intent.getAction().equals(ACTION_LOCAL_BROADCAST)) {  
  8.             String data = intent.getExtras().getString("some_data");  
  9.             Toast.makeText(context, data + " received", Toast.LENGTH_SHORT).show();  
  10.         }  
  11.     }  
  12. }  

MyActivity

This is the code for creating an instance of LocalBroadcastManager and registering MyReceiver. I have a button called “Send Broadcast” that is responsible to send the broadcast whenever clicked.

  1. public class MyActivity extends AppCompatActivity {  
  2.     private LocalBroadcastManager mLocalBroadcastManager;  
  3.     private MyReceiver mMyReceiver;  
  4.     @Override  
  5.     protected void onCreate(Bundle savedInstanceState) {  
  6.         super.onCreate(savedInstanceState);  
  7.         setContentView(R.layout.activity_my);  
  8.         mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);  
  9.         // Creating an intent filter with custom action  
  10.         IntentFilter intentFilter = new IntentFilter();  
  11.         intentFilter.addAction(MyReceiver.ACTION_LOCAL_BROADCAST);  
  12.         // Registering broadcast receiver  
  13.         mMyReceiver = new MyReceiver();  
  14.         mLocalBroadcastManager.registerReceiver(mMyReceiver, intentFilter);  
  15.         // Button click to send broadcast  
  16.         findViewById(R.id.button_send).setOnClickListener(new View.OnClickListener() {  
  17.             @Override  
  18.             public void onClick(View v) {  
  19.                 // Creating a intent with action  
  20.                 Intent intent = new Intent(MyReceiver.ACTION_LOCAL_BROADCAST);  
  21.                 // We can send data also  
  22.                 intent.putExtra("some_data""My data");  
  23.                 // Sending broadcast  
  24.                 mLocalBroadcastManager.sendBroadcast(intent);  
  25.             }  
  26.         });  
  27.     }  
  28.     @Override  
  29.     protected void onDestroy() {  
  30.         super.onDestroy();  
  31.         // Unregistering receiver  
  32.         if (mLocalBroadcastManager != null) {  
  33.             mLocalBroadcastManager.unregisterReceiver(mMyReceiver);  
  34.         }  
  35.     }  
  36. }  
If you face any problem implementing this, please write to me via the comments section. 

Thank you!