Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LocalBroadcastManager vs Context.registerReceiver(), Context.sendBroadcast(Intent), and Context.unregisterReceiver() are they same?

People also ask

What is the use of context sendBroadcast () method?

The sendBroadcast() method allows to send Broadcast Intents. You cannot trigger system Broadcasts, the Android system will prevent this. But you can define intent-filters for your own actions and trigger them via the sendBroadcast() method.

Is LocalBroadcastManager deprecated?

localbroadcastmanager has been fully deprecated. There will be no further releases of this library. Developers should replace usages of LocalBroadcastManager with other implementations of the observable pattern. Depending on the use case, suitable options may be LiveData or reactive streams.

What is broadcast intent in Android?

The Android system automatically sends broadcasts when various system events occur, such as when the system switches in and out of airplane mode. The system sends these broadcasts to all apps that are subscribed to receive the event.

What is local broadcast receiver?

"A Broadcast receiver is an Android component which allows you to register for system or application events." LocalBroadcastManager is a way to send or receive broadcasts within an application process. This mechanism has a lot of advantages.


LocalBroadcastManager is as its name says, an implementation of the broadcast methods that are only available to your app. This has some benefits, with the biggest being safety, one less hole to watch out for. In terms of implementation, there are a few things to keep in mind:

  • This class is from the Android Support Library
  • The method calls have to be prefaced with LocalBroadcastManager.getInstance([CONTEXT]) where [CONTEXT] is a subclass of the Context class, such as Activity.
  • When you use this class, it is purely for your app. Using it to register receivers and make broadcasts that are system wide will fail.

So this class is not the same as Context, it is simply a very specific, app-only implementation of Context's receiver/broadcast methods. You should use it when there is absolutely no point for your listener to listen on global (system-wide) broadcasts and when your broadcast does not need to target anything outside your app.