How to use LocalBroadcastManager? How to use LocalBroadcastManager? android android

How to use LocalBroadcastManager?


I'll answer this anyway. Just in case someone needs it.

ReceiverActivity.java

An activity that watches for notifications for the event named "custom-event-name".

@Overridepublic void onCreate(Bundle savedInstanceState) {  ...  // Register to receive messages.  // We are registering an observer (mMessageReceiver) to receive Intents  // with actions named "custom-event-name".  LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,      new IntentFilter("custom-event-name"));}// Our handler for received Intents. This will be called whenever an Intent// with an action named "custom-event-name" is broadcasted.private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {  @Override  public void onReceive(Context context, Intent intent) {    // Get extra data included in the Intent    String message = intent.getStringExtra("message");    Log.d("receiver", "Got message: " + message);  }};@Overrideprotected void onDestroy() {  // Unregister since the activity is about to be closed.  LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);  super.onDestroy();}

SenderActivity.java

The second activity that sends/broadcasts notifications.

@Overridepublic void onCreate(Bundle savedInstanceState) {  ...  // Every time a button is clicked, we want to broadcast a notification.  findViewById(R.id.button_send).setOnClickListener(new View.OnClickListener() {    @Override    public void onClick(View v) {      sendMessage();    }  });}// Send an Intent with an action named "custom-event-name". The Intent sent should // be received by the ReceiverActivity.private void sendMessage() {  Log.d("sender", "Broadcasting message");  Intent intent = new Intent("custom-event-name");  // You can also include some extra data.  intent.putExtra("message", "This is my message!");  LocalBroadcastManager.getInstance(this).sendBroadcast(intent);}

With the code above, every time the button R.id.button_send is clicked, an Intent is broadcasted and is received by mMessageReceiver in ReceiverActivity.

The debug output should look like this:

01-16 10:35:42.413: D/sender(356): Broadcasting message01-16 10:35:42.421: D/receiver(356): Got message: This is my message! 


I'd rather like to answer comprehensively.

  1. LocalbroadcastManager included in android 3.0 and above so you haveto use support library v4 for early releases. see instructionshere

  2. Create a broadcast receiver:

    private BroadcastReceiver onNotice= new BroadcastReceiver() {    @Override    public void onReceive(Context context, Intent intent) {        // intent can contain anydata        Log.d("sohail","onReceive called");        tv.setText("Broadcast received !");    }};
  3. Register your receiver in onResume of activity like:

    protected void onResume() {        super.onResume();        IntentFilter iff= new IntentFilter(MyIntentService.ACTION);        LocalBroadcastManager.getInstance(this).registerReceiver(onNotice, iff);    }//MyIntentService.ACTION is just a public static string defined in MyIntentService.
  4. unRegister receiver in onPause:

    protected void onPause() {  super.onPause();  LocalBroadcastManager.getInstance(this).unregisterReceiver(onNotice);}
  5. Now whenever a localbroadcast is sent from applications' activity orservice, onReceive of onNotice will be called :).

Edit: You can read complete tutorial here LocalBroadcastManager: Intra application message passing


On Receiving end:

  • First register LocalBroadcast Receiver
  • Then handle incoming intent data in onReceive.

      @Override  protected void onCreate(Bundle savedInstanceState) {      super.onCreate(savedInstanceState);      LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);      lbm.registerReceiver(receiver, new IntentFilter("filter_string"));  }  public BroadcastReceiver receiver = new BroadcastReceiver() {      @Override      public void onReceive(Context context, Intent intent) {          if (intent != null) {              String str = intent.getStringExtra("key");              // get all your data from intent and do what you want           }      }  };

On Sending End:

   Intent intent = new Intent("filter_string");   intent.putExtra("key", "My Data");   // put your all data using put extra    LocalBroadcastManager.getInstance(this).sendBroadcast(intent);