Receiving cross-app broadcasts on Android

This is a cross-app communication example using BroadcastReceiver, one of Android’s four major components. It uses two separate projects to demonstrate: App1 sends a custom broadcast, and App2 receives it.

1. Core Concepts

Android broadcast is a system-level publish/subscribe messaging mechanism:

  1. Broadcasts can be sent and received across applications; you can receive both custom broadcasts and system broadcasts (such as boot completed, battery changes, screen lock).
  2. Testing approach: Create 2 separate Android projects
    • SendDemo: The sender app that sends a custom broadcast.
    • SendDemo2: The receiver app that listens for the broadcast (this project in the screenshot).

2. Receiver Code (SendDemo2)

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

public class MyBroadCastService extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // Callback triggered when broadcast is received; show a Toast
        Toast.makeText(context, "Custom broadcast 22222 received", Toast.LENGTH_LONG).show();
    }
}
Code language: JavaScript (javascript)
  • Must extend BroadcastReceiver and override onReceive(). This method is executed automatically when a broadcast is received.
  • Note: Do not perform time-consuming operations in onReceive, as the broadcast lifecycle is very short.

3. Registering the Broadcast Receiver (Two Methods)

Method 1: Static Registration (register in AndroidManifest.xml; can receive even if the app isn’t running, but restricted after Android 8.0)
<receiver android:name=".MyBroadCastService">
    <intent-filter>
        <!-- Action identifier; the sender must use the exact same action string -->
        <action android:name="com.foxdevelop.MY_CUSTOM_BROADCAST"/>
    </intent-filter>
</receiver>
Code language: HTML, XML (xml)

When SendDemo sends the broadcast, the Intent’s action must be set to com.foxdevelop.MY_CUSTOM_BROADCAST. Both sides must match for the broadcast to be received.

Method 2: Dynamic Registration (register in code; only works while the app is running)
MyBroadCastService receiver = new MyBroadCastService();
IntentFilter filter = new IntentFilter("com.foxdevelop.MY_CUSTOM_BROADCAST");
registerReceiver(receiver, filter);
Code language: JavaScript (javascript)

4. Sender (SendDemo) Broadcast Sending Code

// Send a custom cross-app broadcast
Intent intent = new Intent("com.foxdevelop.MY_CUSTOM_BROADCAST");
sendBroadcast(intent);
Code language: JavaScript (javascript)

5. Important Notes

  1. After Android 8.0 (API 26): Statically registered custom implicit broadcasts are restricted; many custom actions registered statically will not receive broadcasts.
    • Solutions: ① Use dynamic registration; ② Include the package name when sending the broadcast, using an explicit Intent.
  2. Most system broadcasts (network changes, battery) can still be received normally, though some system broadcasts also have restrictions.
  3. Cross-app broadcasts pose security risks; you can set permissions to control who can send and receive them.

6. Runtime Flow

  1. Install both SendDemo (sender) and SendDemo2 (receiver) on the same device/emulator.
  2. In SendDemo, tap a button to call sendBroadcast with the action com.foxdevelop.MY_CUSTOM_BROADCAST.
  3. The system dispatches the broadcast; any BroadcastReceiver registered for that action (MyBroadCastService in SendDemo2) triggers its onReceive.
  4. SendDemo2 displays a Toast: Custom broadcast 22222 received.

Receiving cross-app broadcasts on Android

Leave a Reply

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