Local broadcast events with LocalBroadcastManager in Android

By | March 1, 2016

LocalBroadcastManager is Helper to register for and send broadcasts of Intents to local objects within your process. This is has a number of advantages over sending global broadcasts with sendBroadcast(Intent):

  • You know that the data you are broadcasting won’t leave your app, so don’t need to worry about leaking private data.
  • It is not possible for other applications to send these broadcasts to your app, so you don’t need to worry about having security holes they can exploit.
  • It is more efficient than sending a global broadcast through the system.

LocalBroadcastManager Android

Here is the simple code to demonstrate LocalBroadcastManager.

package com.coderzheaven.localbroadcastmanagerdemo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

    public void onClick(View view) {
        Intent intent = new Intent("Demo");
        intent.putExtra("message", "coderzheaven");

    public void onResume() {

        // Register mMessageReceiver to receive messages.
                new IntentFilter("Demo"));

    // handler for received Intents for the "Demo" event
    private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String message = intent.getStringExtra("message");
            Log.d("receiver", "Received message: " + message);
            ((TextView)findViewById(R.id.textView1)).setText("Received : "+ message);

    protected void onPause() {


Leave a Reply

Your email address will not be published. Required fields are marked *