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.
- public class MyReceiver extends BroadcastReceiver {
- public static final String ACTION_LOCAL_BROADCAST = "com.example.LOCAL";
- @Override
- public void onReceive(Context context, Intent intent) {
-
-
- if (intent.getAction().equals(ACTION_LOCAL_BROADCAST)) {
- String data = intent.getExtras().getString("some_data");
- Toast.makeText(context, data + " received", Toast.LENGTH_SHORT).show();
- }
- }
- }
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.
- public class MyActivity extends AppCompatActivity {
- private LocalBroadcastManager mLocalBroadcastManager;
- private MyReceiver mMyReceiver;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_my);
- mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);
-
- IntentFilter intentFilter = new IntentFilter();
- intentFilter.addAction(MyReceiver.ACTION_LOCAL_BROADCAST);
-
- mMyReceiver = new MyReceiver();
- mLocalBroadcastManager.registerReceiver(mMyReceiver, intentFilter);
-
- findViewById(R.id.button_send).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
-
- Intent intent = new Intent(MyReceiver.ACTION_LOCAL_BROADCAST);
-
- intent.putExtra("some_data", "My data");
-
- mLocalBroadcastManager.sendBroadcast(intent);
- }
- });
- }
- @Override
- protected void onDestroy() {
- super.onDestroy();
-
- if (mLocalBroadcastManager != null) {
- mLocalBroadcastManager.unregisterReceiver(mMyReceiver);
- }
- }
- }
If you face any problem implementing this, please write to me via the comments section.