Android Local Broadcast

Android Local Broadcast (LocalBroadcastManager)

Important note: LocalBroadcastManager has been deprecated in AndroidX. The official recommendation is to use LiveData or Flow for in-app communication, but it is still frequently tested in interviews.

1. What is Local Broadcast (compared to Global Broadcast)

Global Broadcast (sendBroadcast / sendOrderedBroadcast as covered earlier)

  • Goes through the system AMS, cross-application, all apps can receive this broadcast
  • Security risk: Data can be easily intercepted by other apps; other apps can also forge broadcasts to attack our app

Local Broadcast — LocalBroadcastManager

  • Broadcast only circulates within the current app and does not go through the system global broadcast mechanism; other apps cannot receive it at all ✅ Advantages:
  1. Secure: Broadcast does not leak to external apps, and external apps cannot send fake broadcasts into our app
  2. Efficient: No cross-process or system AMS interaction needed, making it faster
  3. No need to declare permissions Limitations:
  4. Dynamic registration only; static registration (declaring receiver in AndroidManifest) is invalid
  5. Only works within the same app; cannot cross processes or apps
  6. Cannot be used together with system broadcasts

2. Complete Code Breakdown

2.1 Custom Receiver MyReceiver

public class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent)
    {
        Toast.makeText(context, "Local broadcast received", Toast.LENGTH_LONG).show();
    }
}
Code language: PHP (php)

2.2 Using LocalBroadcastManager in Activity

package cn.bamn.www.localcastdmeo;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private LocalBroadcastManager localBroadcastManager; // Reference to local broadcast manager
    private IntentFilter intentFilter;
    private MyReceiver myReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Get the LocalBroadcastManager singleton
        localBroadcastManager = LocalBroadcastManager.getInstance(this);

        Button button = (Button) findViewById(R.id.btn_send);
        button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                // Build Intent action and send local broadcast
                Intent intent = new Intent("com.foxdevelop.www.LocalCastDemo.LOCAL_BROADCAST");
                localBroadcastManager.sendBroadcast(intent);
            }
        });

        // Dynamically register the local broadcast receiver
        intentFilter = new IntentFilter();
        intentFilter.addAction("com.foxdevelop.www.LocalCastDemo.LOCAL_BROADCAST");
        myReceiver = new MyReceiver();
        localBroadcastManager.registerReceiver(myReceiver,intentFilter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // Must unregister! Prevent memory leaks
        localBroadcastManager.unregisterReceiver(myReceiver);
    }
}
Code language: JavaScript (javascript)

Core APIs:

  • LocalBroadcastManager.getInstance(context): Get the instance (singleton)
  • registerReceiver(receiver, intentFilter): Dynamically register a receiver
  • sendBroadcast(intent): Send an unordered local broadcast; sendOrderedBroadcast() for ordered local broadcast
  • unregisterReceiver(): Unregister when the page is destroyed — must be written, a major memory leak pitfall

3. Comparison Summary: Local Broadcast vs. Unordered Global Broadcast vs. Ordered Global Broadcast

TypeScopeCross-AppCan be abortedRegistrationUnderlying Mechanism
Global Unordered Broadcast sendBroadcastGlobalYesCannot abortStatic + DynamicSystem AMS cross-process
Global Ordered Broadcast sendOrderedBroadcastGlobalYesabortBroadcast to abortStatic + DynamicSystem AMS cross-process, serial by priority
Local Broadcast LocalBroadcastManagerWithin appNoSupports ordered broadcast abortDynamic onlyIn-memory dispatch, no AMS, high efficiency

Android Local Broadcast

Leave a Reply

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