The broadcast Manager is quite similar to the Publish–subscribe Design Pattern. For example — when I subscribe to some of the events or follow someone, whenever new things will come up it automatically notifies us.
In terms of BR if any system event will trigger for ex-Airplane Mode, battery low…etc if our app needs to listen to this event, here we will use the broadcast receiver.
Android Apps Can Send or receive the broadcast message from Android Systems or other Apps.
The broadcast message itself is wrapped in an Intent object whose action string identifies the event which needs to be executed (for example
There are two ways to register a broadcast receiver-
Manifest-declared (Static): By this receiver can be registered via the AndroidManifest.xml file.
Context-registered (Dynamic): By this, register a receiver dynamically via the Context.registerReceiver() method.
To be able to receive a broadcast, the application has to extend the Broadcast Receiver abstract class and override its onReceive() method.
If the event for which the broadcast receiver has registered happens, the onReceive() method of the receiver is called by the Android system.
Sending Broadcast EVENT-
1. Global Broadcast Manager - If Communication Occurs between two Apps, here we can use Global Broadcast Receiver.
2. Local Broadcast Manager - If the communication is limited to one app, here it is recommended to use a local Broadcast Receiver.
what is the main difference and when to use local and global BR-
⏺ Ans - there is some security hole if we go with the Global Broadcast Manager. We should avoid sharing sensitive Data in Global Broadcast. Also, Global broadcast is available system-wide, so it is not good for performance as well.
The Local Broadcast is more efficient and compatible with our use case. It doesn't need interprocess communication.
The benefit of Local Broadcast Receiver —
● Not Launched on the System-wide● The more efficient way to send the BR as compared to Global Broadcast● Broadcast data won’t leave the App, so a Sensitive Data leak is not possible.● It is not possible for other applications to send these broadcasts to our app, so we don’t need to worry about having security holes they can exploit.
Best Practice to Use Broadcast Receiver
- If we don't need to send BR outside our app, then we can use LocalBroadcastManager to send or receive the Data.
- If many apps have registered to receive the same broadcast in their manifest, it can cause the system to launch a lot of apps, in this case, device performance and user experience will be affected. To avoid this, we can use context registration over the manifest declaration.
- We should not share Sensitive information using implicit Intent. The information can be read by any app that registers to receive the broadcast.
We have some ways to overcome this issue-
- We can specify permission when registering a broadcast receiver.
- For manifest-declared receivers, we can set the android: exported attribute to “false” in the manifest. The receiver does not receive broadcasts from sources outside the app.
- We can limit our app to only local broadcasts receiver.
- We should not start an activity from the BR because User Experience can be unexpected.
Conclusion-we can use both the BR as per our use case but only for Global Broadcast, we need to ensure that there should no security holes that can leak our information to other apps.
If we want BR in our apps, then we should use the concept of LocalBroadcastManager instead of Global.