Broadcast Receivers are one of the core Android components used for listening to system-wide events and application-level broadcasts.
Android applications use Broadcast Receivers for:
- Battery status monitoring
- Internet connectivity changes
- SMS reception
- Boot completed events
- Custom app events
- Push notification triggers
In this tutorial, we will learn:
- What Broadcast Receivers are
- Types of broadcasts
- How Broadcast Receivers work
- How to register receivers
- Modern Android broadcast limitations
- Best practices and security improvements
What Is a Broadcast Receiver?
Broadcast Receiver is an Android component that listens for:
- System-wide events
- Application broadcasts
- Custom intents
It follows Android’s publish-subscribe event mechanism.
The receiver remains inactive until a subscribed event occurs.
Examples of Android System Broadcasts
Android continuously broadcasts events such as:
- Battery low
- SMS received
- Phone call received
- Internet connectivity changes
- Device boot completed
- Screen turned on/off
Applications can listen for these broadcasts using Broadcast Receivers.
Important Characteristic of Broadcast Receivers
Broadcast Receivers:
- Do not have UI
- Do not continuously run in memory
- Execute only when triggered
- Can launch Activities or Services
Types of Broadcasts in Android
There are two main types of broadcasts:
- Normal Broadcasts
- Ordered Broadcasts
1. Normal Broadcasts
Normal broadcasts are asynchronous broadcasts.
Characteristics:
- Delivered to all receivers simultaneously
- Receivers may execute in any order
- Efficient and fast
- Receivers cannot modify results
- Receivers cannot abort broadcasts
Send Normal Broadcast Example
sendBroadcast(intent);
2. Ordered Broadcasts
Ordered broadcasts are synchronous broadcasts.
Characteristics:
- Delivered one receiver at a time
- Receivers execute based on priority
- Each receiver can pass result to next receiver
- Receivers can abort broadcasts
Send Ordered Broadcast Example
sendOrderedBroadcast(
intent,
null
);
Broadcast Receiver Priority
Ordered broadcast execution order is controlled using:
android:priority
inside:
intent-filter
Priority Example
<receiver
android:name=".MyReceiver">
<intent-filter
android:priority="100">
<action
android:name=
"android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
Benefits of Broadcast Receivers
- Application can react to system events
- No continuous background execution needed
- Efficient event-driven architecture
- Can wake up application when required
- Supports inter-application communication
Important Performance Limitation
Broadcast Receivers should complete execution quickly.
Android imposes approximately:
10 seconds
execution limit.
Heavy operations should NOT be performed directly inside BroadcastReceiver.
Avoid Heavy Operations Inside BroadcastReceiver
Avoid:
- Large database operations
- Long network calls
- Heavy file processing
- Complex computations
Instead use:
- WorkManager
- Foreground Services
- JobScheduler
- Coroutines
Creating a Simple Broadcast Receiver
Step 1 — Create MyReceiver.java
package com.example.broadcastdemo;
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,
"Broadcast Received",
Toast.LENGTH_LONG
).show();
}
}
Register Receiver in AndroidManifest.xml
<receiver
android:name=".MyReceiver"
android:exported="false">
<intent-filter>
<action
android:name=
"com.example.MY_ACTION"/>
</intent-filter>
</receiver>
Why android:exported Is Important
Modern Android versions require:
android:exported
for security purposes.
- true → accessible externally
- false → internal app only
Send Custom Broadcast
Intent intent =
new Intent(
"com.example.MY_ACTION"
);
sendBroadcast(intent);
How Broadcast Receiver Works
- System or app sends broadcast
- Android searches matching receivers
- Receiver gets activated
- onReceive() executes
- Receiver becomes inactive again
Manifest Receiver vs Dynamic Receiver
| Manifest Receiver | Dynamic Receiver |
|---|---|
| Declared in Manifest | Registered at runtime |
| Can work when app closed | Works while app active |
| Less flexible | More dynamic control |
Dynamic Broadcast Receiver Example
MyReceiver receiver =
new MyReceiver();
IntentFilter filter =
new IntentFilter(
ConnectivityManager
.CONNECTIVITY_ACTION
);
registerReceiver(
receiver,
filter
);
Unregister Dynamic Receiver
unregisterReceiver(receiver);
Modern Android Broadcast Restrictions
Starting from Android 8.0 (Oreo), implicit broadcast limitations were introduced to improve:
- Battery performance
- Background execution limits
- System efficiency
Modern Alternatives
Modern Android applications commonly prefer:
- WorkManager
- Foreground Services
- Lifecycle-aware observers
- Reactive architecture
Common Broadcast Receiver Use Cases
- Internet connectivity monitoring
- Battery level detection
- Push notification handling
- Boot completed startup
- Alarm triggers
- SMS listening
Broadcast Receiver vs Service
| Broadcast Receiver | Service |
|---|---|
| Short-lived component | Long-running component |
| Event-driven | Background processing |
| No UI | No UI |
| Executes quickly | Can run longer |
Security Best Practices
- Use explicit broadcasts where possible
- Set android:exported carefully
- Avoid exposing sensitive broadcasts
- Validate incoming intents
Common Beginner Mistakes
1. Doing Heavy Work Inside onReceive()
BroadcastReceiver should finish quickly.
2. Forgetting unregisterReceiver()
Dynamic receivers can cause memory leaks if not unregistered.
3. Using Deprecated Broadcasts
Some system broadcasts are restricted in newer Android versions.
FAQ
What is Broadcast Receiver in Android?
Broadcast Receiver listens for system or application broadcast events.
Can Broadcast Receiver run in background?
Yes, but execution time is limited.
What is the modern replacement?
WorkManager and lifecycle-aware event systems are commonly preferred today.
Conclusion
Broadcast Receivers are powerful Android components for handling system-wide and application-level events.
They provide efficient event-driven communication while keeping applications lightweight and responsive.
Modern Android applications should combine Broadcast Receivers with WorkManager, AndroidX libraries, and lifecycle-aware architectures for scalable production-grade implementations.
About the Author
Salil Jha is a Full Stack and Mobile Developer specializing in Android, React Native, fintech systems, scalable SaaS platforms, and developer tooling products.
CodeChain Dev — Build Modern Products. Solve Real Problems.
No comments:
Post a Comment