Android Local Broadcast Manager Example

Android global broadcast make different android apps communication easy and clear, but it also has below security issues.

  1. Any android apps can send same broadcast intent action which your application broadcast receiver listen. This issue can make your app being attacked.
  2. Any android apps can listen to your app’s broadcast receiver registered action. So your data maybe intercepted by those apps.

To resolve above security issue, android provide LocalBroadcastManager class which can make the broadcast sent and received internally just in your app.

1. LocalBroadcastManager Example.

This example contains two android apps App One and App Two. They are created in different android project, and executed in same android emulator.

The source code of the two apps are almost similar. All have a button, when user click the button, it will send a broadcast to registered broadcast receiver.

If send global broadcast then all the broadcast receiver registered in App One and App Two can receive the broadcast, and each app will show a toast popup message at screen bottom as below.

android send same broadcast in two acivity

If send local broadcast use LocalBroadcastManager, then each app can only receive the broadcast that sent by the app locally shown in below picture.

2. How To Use LocalBroadcastManager To Send Local Broadcast.

  1. Get an instance of LocalBroadcastManager.

    localBroadcastManager = LocalBroadcastManager.getInstance(this);
  2. Create a broadcast receiver and register it use LocalBroadcastManager object.
    localBroadcastManager.registerReceiver(receiverTwo, intentFilter);
  3. Use LocalBroadcastManager instance to send broadcast.
  4. Do not forget unregister broadcast receiver use LocalBroadcastManager.


3. LocalBroadcastManager Example Source Code.

To run this example, you need to create two android projects. One project includes one android app.

You should start one android emulator and run the two apps in the same emulator.

two android projects for localbroadcastmanager example

3.1 Project One App One Source Files.

3.1.1 Layout Xml File.


    android:text="Send Broadcast In Activity One"/>
3.1.2 Activity Java File.

package com.dev2qa.example.broadcast.activity;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.dev2qa.example.R;
import com.dev2qa.example.broadcast.receiver.LocalBroadcastReceiverOne;

public class LocalBroadcastOneActivity extends AppCompatActivity {

    public static final String LOCAL_BROADCAST_ACTION = "com.dev2qa.example.broadcast.activity.LOCAL_BROADCAST";


    private LocalBroadcastManager localBroadcastManager = null;

    private LocalBroadcastReceiverOne receiverOne = null;

    protected void onCreate(Bundle savedInstanceState) {

        setTitle("App One. - Local Broadcast Manager Example.");

        // Get local broadcast manager object.
        localBroadcastManager = LocalBroadcastManager.getInstance(this);

        // Create local broadcast receiver one.
        receiverOne = new LocalBroadcastReceiverOne();

        // Create intent filter, add filter action.
        IntentFilter intentFilter = new IntentFilter();

        // Register receiver one to local broadcast manager.
        localBroadcastManager.registerReceiver(receiverOne, intentFilter);
        //registerReceiver(receiverOne, intentFilter);

        Button sendBroadcastOneButton = (Button)findViewById(;
        sendBroadcastOneButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                // When being clicked, it will send a local broadcast with the sender info.
                Intent intent = new Intent(LOCAL_BROADCAST_ACTION);
                intent.putExtra(LOCAL_BROADCAST_SOURCE, "Activity One");

    protected void onDestroy() {
            // Do not forget unregister the local broadcast receiver in onDestroy method.
3.1.3 Broadcast Receiver One Java File.

package com.dev2qa.example.broadcast.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

import com.dev2qa.example.broadcast.activity.LocalBroadcastOneActivity;

 * Created by Jerry on 1/7/2018.

public class LocalBroadcastReceiverOne extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if(LocalBroadcastOneActivity.LOCAL_BROADCAST_ACTION.equals(action)) {
            String fromActivity = intent.getStringExtra(LocalBroadcastOneActivity.LOCAL_BROADCAST_SOURCE);
            Toast.makeText(context, "Receiver one receive broadcast from " + fromActivity, Toast.LENGTH_LONG).show();
3.1.4 AndroidManifest.xml File.
<manifest xmlns:android=""


        <activity android:name=".broadcast.activity.LocalBroadcastOneActivity">
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />



App Two’s source code is very similar to App One, but just in different android project.

2 thoughts on “Android Local Broadcast Manager Example”

  1. what is the use of checking if(LocalBroadcastOneActivity.LOCAL_BROADCAST_ACTION.equals(action)) in,because you used intentFilter.addAction(LOCAL_BROADCAST_ACTION) in which only listens to LOCAL_BROADCAST_ACTION?

    1. This line of code is for general purpose, because in real android application, there are multiple actions in general. So it is a good habit to check the action value. Another reason is that this can avoid spam attack. If you do not check the action value, then any action can invoke the code. So the invoker app do not need to guess the action value at all.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.