Android Broadcast Manager

Prachi Mishra
3 min readOct 30, 2022

--

Stolen from Google

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 android.intent.action.AIRPLANE_MODE).

Register Broadcast

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.

Receive Broadcasts

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 BroadcastBroadcast 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-

  1. We can specify permission when registering a broadcast receiver.
  2. 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.
  3. We can limit our app to only local broadcasts receiver.
  4. 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.

Happy coding…😀

--

--

Prachi Mishra

I am working As an Android Dev. Currently, developing an App for my Dream car (BMW) .In my Dev path If I find something interesting I like to share it with you.