0

Are there any use cases in which I would want to use a BroadcastReceiver for something other than cross-application communication?

After reading the documentation, it seems like they are targeted at cross-application communication, but the idea of using them with the LocalBroadcastManager is also mentioned. I also read this post, which addresses the general usage of broadcast receivers. Neither seem to hit clearly on why it would be useful to use broadcasts and receivers for anything other than cross-application communication.

Does it have anything to do with their asynchronous nature, or maybe they're just used to move some processing out of the main activity?

Clarification: I guess the term cross-application was too general. I was considering the built-in actions such as

android.hardware.action.NEW_PICTURE

to be coming from applications. What I would like to know is: when is it useful to use a BroadcastReceiver for communication within my app - I guess this would narrow it down to custom intent actions/categories. Sorry if the way I'm asking this is confusing. I've only just recently begun programming with Android and I still don't understand the OS very well.

Community
  • 1
  • 1
Mack
  • 2,614
  • 2
  • 21
  • 33

1 Answers1

0

As it quotes in the post you linked: "A broadcast receiver is a component that responds to system-wide broadcast announcements. Many broadcasts originate from the system."

Therefore, they are useful in "catching" system broadcasts, e.g., when the device has booted up, when the battery level changes, when your Wi-Fi has been turned on, etc.

Personally, I've used a BroadcastReceiver to start my app whenever the device receives a text message. In my case, this is preferred, as my app does not need to be running when the text message is received. Upon receipt, the system will broadcast an intent with an SMS_RECEIVED action. My BroadcastReceiver component, registered to accept this particular type ofbroadcast, will be notified and can then react as needed. In my case, it launches an Activity to notify the user of the text message and choose a particular reply text message.

This is just a specific example. There are many other broadcasts that the system transmits and your Receiver can get. Check this link and this post for more examples.

Clarification:

A LocalBroadcastManager is preferable in instances when you want rather simple way to communicate between app components without an intent being broadcast system-wide. Knowing this, you would want to go this route when the info you're passing is to be used only by your app. A LocalBroadcastManager is more efficient, generally simpler than using an IBinder interface, and can ensure that information isn't leaving your app.

For an example, suppose you have a Service that runs in the background to track and log some constantly changing data; let's say your battery level. The Service can run on its own without user interaction, saving the battery level to the disk as it changes. When you start your activity to view said data, it would use LocalBroadcastManager to register a receiver to accept the info the Service is sending, and update the Activity's UI in real time to reflect this. Since no other app needs the info from your Service, it's better to do this than broadcasting an intent to which any other app could have access.

To sum up, using a LocalBroadcastManager is:

  1. More efficient.
  2. More secure.
  3. Simpler.
Community
  • 1
  • 1
Mike M.
  • 38,532
  • 8
  • 99
  • 95
  • Thanks for your response; I believe you addressed the question I asked, but I now think what I asked was not what I wanted an answer to. I've updated my question to clarify my intent. Can you take a look? – Mack Oct 21 '13 at 19:10