Send Custom Broadcast

1. Custom Broadcast Receiver MyBroadcastReceiver.java

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class MyBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Received our custom broadcast", Toast.LENGTH_LONG).show();
    }
}
Code language: JavaScript (javascript)

2. Register Receiver in AndroidManifest.xml (Manifest File)

Add inside the <application> tag:

<receiver
    android:name=".MyBroadcastReceiver"
    android:enabled="true"
    android:exported="true">
    <intent-filter>
        <action android:name="com.foxdevelop.www.broadcastdemo.MY_BROADCAST" />
    </intent-filter>
</receiver>
Code language: HTML, XML (xml)

Key point: The action name. The Intent string used when sending broadcasts later must match exactly with this value

3. activity_main.xml Layout (Button)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/btn_send"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Send Broadcast"/>
</LinearLayout>
Code language: HTML, XML (xml)

4. MainActivity.java Main Activity (Send Broadcast On Button Click)

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = findViewById(R.id.btn_send);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // The action string must exactly match the action defined in Manifest
                Intent intent = new Intent("cn.bamn.www.broadcastdemo.MY_BROADCAST");
                sendBroadcast(intent);
            }
        });
    }
}
Code language: JavaScript (javascript)

Normal Broadcast vs Ordered Broadcast

  1. Normal Broadcast sendBroadcast() Asynchronous broadcast. All receivers receive it nearly at the same time, unordered and cannot be aborted.
  2. Ordered Broadcast sendOrderedBroadcast() Receivers get broadcasts sequentially by priority. Earlier receivers can call abortBroadcast() to terminate the broadcast, and subsequent receivers will not receive it.

Error shown in screenshot: Activity class ...MainActivity does not exist

Error meaning: The system cannot find the MainActivity class

Common Causes

  1. Incorrect path for Activity android:name in AndroidManifest.xml (Most Common)
    • Mismatched package name and class name, wrong case
    • How it works: android:name=".MainActivity". The dot . means under the current application package
    • Verification method: Hold Ctrl and click the class name in IDE. If it jumps to MainActivity, the path is correct
  2. Package name mismatch: applicationId in build.gradle does not match package name in manifest
  3. Missing class file: MainActivity.java deleted / wrong filename (Java class name must match filename exactly, case-sensitive)
  4. Build failure: Project compilation failed, class file not generated; clean and rebuild project (Build -> Clean Project)
  5. Case sensitivity: Mainactivity and MainActivity are two separate classes, Android is case-sensitive

Simple Troubleshooting Steps

  1. Open AndroidManifest.xml and locate the launcher Activity node<activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
  2. Hold Ctrl and click .MainActivity to check if code navigation works
  3. Cannot jump → Update android:name to the fully qualified correct class name
  4. Can jump → Run Clean Project, rebuild and reinstall apk

Note: For newer Android versions (API26+): Static registration of implicit broadcasts has many restrictions. Many system implicit broadcasts cannot be statically registered. However, custom action implicit broadcasts are exempt from this restriction and work normally. This demo can run directly.

Send Custom Broadcast

Leave a Reply

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