Android7/8/2018

Understanding Broadcast Receivers in Android

Broadcast Receivers are one of the core Android components used to listen for system-wide events and application-level broadcasts.

Android applications use Broadcast Receivers for:

  • Incoming phone calls
  • SMS received events
  • Battery status updates
  • Wi-Fi state changes
  • Custom application events

In this tutorial, we will learn how Broadcast Receivers work in Android, how to register them, and how to create custom broadcast events.


What Is a Broadcast Receiver?

A Broadcast Receiver is an Android component that listens for broadcast messages sent by:

  • Android operating system
  • Other applications
  • Your own application

When a matching broadcast event occurs, Android automatically triggers the receiver’s onReceive() method.


Common Use Cases of Broadcast Receivers

Event Example Usage
Phone State Changes Track incoming calls
Battery Status Show low battery warnings
Wi-Fi State Detect internet availability
Boot Completed Start background services
Custom Events Internal app communication

Example Scenario

Suppose we want to save incoming call details into a local SQLite database.

We can create a Broadcast Receiver that listens for:


android.intent.action.PHONE_STATE

Whenever the phone state changes, Android automatically triggers our receiver.


Two Ways to Register a Broadcast Receiver

  1. Static Registration (AndroidManifest.xml)
  2. Dynamic Registration (Runtime)

Method 1 — Register Receiver in AndroidManifest.xml

Static registration allows Android to trigger the receiver even if the app is not currently open.

AndroidManifest.xml


<receiver
    android:name=".IncomingCallReceiver">

    <intent-filter>
        <action
            android:name=
            "android.intent.action.PHONE_STATE"/>
    </intent-filter>

</receiver>

Create IncomingCallReceiver.java


package com.example.broadcastreceiver;

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

public class IncomingCallReceiver
        extends BroadcastReceiver {

    @Override
    public void onReceive(
            Context context,
            Intent intent
    ) {

        Toast.makeText(
                context,
                "Phone State Changed",
                Toast.LENGTH_SHORT
        ).show();
    }
}

How Static Registration Works

The workflow is:

  1. Android system broadcasts phone state change
  2. Intent filter matches receiver
  3. Android launches Broadcast Receiver
  4. onReceive() executes automatically

Method 2 — Dynamic Registration (Runtime)

Dynamic registration works only while the application is active.

MainActivity.java


package com.example.broadcastreceiver;

import androidx.appcompat.app.AppCompatActivity;

import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private BroadcastReceiver receiver =
            new BroadcastReceiver() {

                @Override
                public void onReceive(
                        android.content.Context context,
                        Intent intent
                ) {

                    Toast.makeText(
                            context,
                            "Configuration Changed",
                            Toast.LENGTH_SHORT
                    ).show();
                }
            };

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onResume() {

        super.onResume();

        IntentFilter filter =
                new IntentFilter(
                        Intent.ACTION_CONFIGURATION_CHANGED
                );

        registerReceiver(receiver, filter);
    }

    @Override
    protected void onPause() {

        super.onPause();

        unregisterReceiver(receiver);
    }
}

Why unregisterReceiver() Is Important

Failing to unregister dynamically registered receivers can cause:

  • Memory leaks
  • Application crashes
  • Lifecycle issues

Creating Custom Broadcast Events

Applications can also create custom broadcast events.

Step 1 — Register Custom Receiver


<receiver
    android:name=".MyReceiver">

    <intent-filter>
        <action
            android:name=
            "com.example.mybroadcast"/>
    </intent-filter>

</receiver>

Step 2 — Create Receiver Class


package com.example.broadcastreceiver;

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

public class MyReceiver
        extends BroadcastReceiver {

    @Override
    public void onReceive(
            Context context,
            Intent intent
    ) {

        Toast.makeText(
                context,
                "Custom Broadcast Received",
                Toast.LENGTH_SHORT
        ).show();
    }
}

Step 3 — Send Broadcast Event


Intent intent =
        new Intent(
                "com.example.mybroadcast"
        );

sendBroadcast(intent);

How Custom Broadcasts Work

The workflow is:

  1. Application sends custom broadcast
  2. Android checks matching intent filters
  3. Receiver gets triggered
  4. onReceive() executes

Important Limitation of onReceive()

The onReceive() method executes for a short duration only.

You should NOT perform:

  • Long network requests
  • Heavy database operations
  • Background threads

inside onReceive().

For long-running tasks, start:

  • Foreground Service
  • WorkManager
  • JobScheduler

Common Mistakes Developers Make

1. Forgetting Permissions

Some broadcasts require permissions such as:


READ_PHONE_STATE

2. Not Unregistering Receivers

Dynamically registered receivers should always be unregistered.


3. Running Heavy Tasks in onReceive()

Broadcast Receivers should complete quickly.


Modern Android Changes

Modern Android versions restrict many implicit broadcasts to improve battery performance.

Modern applications should prefer:

  • WorkManager
  • Foreground Services
  • Lifecycle-aware components

Broadcast Receiver vs Service

Broadcast Receiver Service
Short-lived component Long-running component
Event listener Background processing
Triggered by broadcasts Runs independently

FAQ

Can Broadcast Receivers run in background?

Yes, but Android imposes restrictions on modern versions for battery optimization.

Can Broadcast Receivers start Activities?

Yes, but background activity launches are restricted in newer Android versions.

What is the modern replacement for many background broadcasts?

WorkManager is the recommended modern solution for scheduled background work.


Conclusion

Broadcast Receivers are an important part of Android event-driven architecture.

They allow applications to respond to system events and custom application broadcasts efficiently.

Modern Android applications should combine Broadcast Receivers with lifecycle-aware background processing solutions for better scalability and performance.


About the Author

Salil Jha is a Full Stack and Mobile Developer with experience in Android, React Native, scalable SaaS platforms, fintech systems, and developer tooling applications.

CodeChain Dev — Build Modern Products. Solve Real Problems.

Deep Structural Diagnostics.

Mastering JSON is only the first step. Use our industrial-grade workbench to format, validate, and synthesize models for your production APIs.