Broadcast Receiver in Android

Creating-a-Broadcast-Receiver-in-Android-feature.jpg

A Broadcast Receiver in Android is a small component that listens for events on the device and reacts to them as they occur. These events can be system actions, such as an incoming call, a new SMS, airplane mode turning on, or even events created within your own app. It allows your application to respond automatically without the user having to open it. In this blog, you will learn what a Broadcast Receiver is, how it works, and how to use it to build more responsive Android applications.

Table of Contents:

What is a Broadcast Receiver in Android?

A BroadcastReceiver is a component that receives broadcast messages (known as Intents) from the Android system or other applications. For example, your system may send a message or notification when the device is connected to Wi-Fi, when the battery is running low, or when the screen goes off. Your app can hear these broadcasts and take proper actions.

Key Features of Broadcast Receiver 

Some of the key features of the Broadcast Receiver in Android include:

  1. It allows your application to monitor important events such as low battery, screen on or off, or your own custom triggers.
  2. It does not show any screen or layout. It runs in the background to handle events.
  3. Your app can react to broadcasts even when the user is not using it.
  4. The receiver is lightweight and focuses only on handling events quickly.
  5. It exists only when an event occurs and completes its work immediately after the event is over.
  6. You can register it in the AndroidManifest file to keep it active all the time, or register it inside your code when you want it to work only while the app is running.
Become a Full-Stack Software Engineer
Master frontend and backend development, build real-world projects, and get job-ready with expert mentors.
quiz-icon

Types of Broadcast Receivers in Android

There are mainly two types of Broadcast Receivers in Android. They are:

1. System Broadcast Receivers

A System Broadcast Receiver receives events sent by the Android system itself. They are system-wide events that automatically occur on the device. Android sends a broadcast, e.g., when the battery is low, when the phone is charging, when the screen is on or off, or when the device is on Wi-Fi. With a system Broadcast Receiver registered to your app, it can identify these events and react to them. These events are generated by the Android operating system, not by the developer.

2. Custom Broadcast Receivers

You can make your own Custom Broadcast Receiver to process your own defined events. These broadcasts are sent by your app and not by the Android system. An example of this is when your app is downloading a file in the background. After the download is complete, your app is able to send a custom broadcast. Your other application component, like the user interface, can listen to this broadcast and present a message on the screen that will state “download complete!”. In this case, you are in control of sending the broadcast as well as creating the receiver to listen to the broadcast.

Lifecycle of a Broadcast Receiver in Android

The lifecycle is short because the receiver does not stay active in the background. It wakes up only when an event is sent and shuts down immediately after handling it. This keeps the system fast and efficient.

Here is the complete cycle of a broadcast receiver in Android:

1. The event is broadcast

A system action, like a low battery, network change, or a custom event from your own app, is sent out. The system checks which receivers are registered for this event.

2. Receiver becomes active

The component is loaded into memory for that moment. It does not create a user interface or run for a long time. Its only purpose here is to respond to the event.

3. onReceive is triggered

The system calls the onReceive method. This is where the event is handled.

Common actions include:

  1. Showing a notification
  2. Updating stored data
  3. Starting a background service for longer operations
  4. Sending another broadcast within the app
  5. Logging the event for analytics

4. Task completes

The method finishes quickly. The receiver is required to avoid long operations because the system expects fast execution. Anything that takes time should be passed to a service or worker.

5. The receiver is destroyed

After onReceive ends, the component is removed from memory. It stays inactive until another matching event occurs.

6. Waits for the next broadcast

It remains idle with no ongoing work. When the next recognized event appears, the same cycle repeats.

How to Create a Broadcast Receiver in Android?

In Android, a Broadcast Receiver in Android is created when you want your application to listen and respond to some events, like when the battery is low, or when the device gets connected to Wi-Fi. In order to create a Broadcast Receiver in Android, you need to follow the steps given below:

Step 1: Create a New Broadcast Receiver Class in Android

At the first step, you have to create a class that extends the BroadcastReceiver class. Inside that class, you have to override the onReceive() method. This method will run whenever your receiver catches an event. 

Example:

Java

Explanation: The above Java code is used to create a Broadcast Receiver that listens for the low battery event. When the system sends the ACTION_BATTERY_LOW broadcast, the onReceive() method runs and displays a small pop-up message that says “Battery is Low!

Get 100% Hike!

Master Most in Demand Skills Now!

Step 2: Register the Receiver

You can register the receiver in two ways:

a. Static Registration (Manifest File)

Here, you have to add the receiver details inside the AndroidManifest.xml file. This way, the receiver remains active and can watch the event, even when your application is not running. It is useful for system-level events like booting the device, battery low, or network changes.

Example:

<receiver android:name=".MyReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BATTERY_LOW"/>
    </intent-filter>
</receiver>

Explanation: The above XML code is used to register MyReceiver in the manifest so that it gets notified about the battery low event. When the battery gets low, Android instructs the receiver to run its onReceive() command.

b. Dynamic Registration (In Code)

You can also register it in your activity or service using an IntentFilter. This method is called dynamic registration, and it only works while your app is running. It gives you more control because you can decide exactly when to start and stop listening to the broadcast.

Example:

Java

You should not forget to unregister the receiver when it is no longer needed (like in onStop() or onDestroy()).

Java

Step 3: Test the Receiver

Now, whenever the battery of your device goes low, your application will notify you with a pop-up message saying “Battery is Low!”. This happens because the Broadcast Receiver is listening to the battery low event actively. As soon as the Android sends that broadcast, your receiver catches it instantly and displays the message to alert you. This helps to make sure that you are already informed about the system changes without opening the app.

Advantages and Limitations of Broadcast Receivers

In this section, let’s discuss the various advantages and limitations of Broadcast Receivers in Android.

Advantages of Broadcast Receivers

  1. You can make your app react to important system events like low battery, Wi-Fi connection, or the screen turning on and off.
  2. Your application does not need to be open; the Broadcast Receiver can still watch the events and take action.
  3. Broadcast Receivers run only for a short period of time. Therefore, they are unable to use too many system resources.
  4. They also allow different applications or the components of the same application to send and receive messages easily.
  5. You can use them to notify the users about important updates like incoming calls, new messages, or reminders.

Disadvantages of Broadcast Receivers

  1. A Broadcast Receiver works only for a moment when the event occurs. Therefore, it is unable to handle long-running tasks.
  2. If you use a lot of Broadcast Receivers, your application might slow down, or it might drain more battery.
  3. In the newer Android versions, many broadcasts are limited in order to save battery and improve the performance of the device. Hence, not all broadcasts work as before.
  4. Broadcast Receivers cannot show a screen directly; they can only trigger actions like sending a notification.
  5. When the Broadcast Receivers are not used carefully, they can make your application respond to unnecessary events. This affects the stability and security of the application.

Sending and Receiving Broadcast Intents in Android

In Android, broadcasts are sent as intents. An intent is basically a message that can be passed to different parts of the system. When a broadcast intent is sent, it can be received either by the Android system, your own application, or even by other applications if they are set up to listen for it.

Sending a Broadcast Intent

In order to send a broadcast, you have to create an Intent along with a specific action, and then you have to call sendBroadcast(). These actions work like a label that helps receivers to identify what kind of event it is.

Example:

Java

Explanation: In the above Java code, the broadcast is sent with the action com.example.CUSTOM_INTENT.

Receiving a Broadcast Intent

In order to receive a broadcast, you need a Broadcast Receiver that listens to the action you have defined. You can register it either in the AndroidManifest.xml or you can do it dynamically in your code using IntentFilter.

Example:

Java

After that, you can register the Broadcast Receiver dynamically:

Example:

Java

Explanation: The above Java code tells your application to listen to the event named “com.example.CUSTOM_INTENT” and then runs MyReceiver when the event is broadcast.

Best Practices for Using Broadcast Receivers in Android

Let us explore the best practices using Broadcast Receivers in Android:

  1. Unregister Dynamic Receivers: Remove receivers registered in code when the activity or service is destroyed. This prevents memory leaks and keeps the app stable.
  2. Use LocalBroadcastManager for Internal Communication: When the message is meant only for your own app, use LocalBroadcastManager. It is faster, safer, and does not involve the entire system.
  3. Avoid heavy work inside onReceive: The onReceive method runs on the main thread. Move long or complex tasks to a background service or worker to keep the app smooth.
  4. Use Static Receivers Only When Needed: Static receivers respond to system events even when the app is not open. Overusing them can drain the battery, so register them only for important events.
  5. Protect Broadcasts with Permissions: When sending sensitive data, apply permissions. This ensures that only trusted apps can send or receive that broadcast.

Real-World Use Cases of Broadcast Receivers in Android

Given below are some of the real-world use cases of Broadcast Receivers in Android:

1. Network State Changes: Broadcast receivers are used to detect when the device is connected or disconnected from the internet.

2. Battery Monitoring: Your device gets alerts when the battery is low or when you plug into a charger.

3. Incoming Calls and SMS Detection: You will get to know when a call or text message arrives on the device.

4. Alarm and Reminders: It is responsible for triggering notifications or tasks at a scheduled time. 

Conclusion

Broadcast Receivers in Android act like messengers that help your app react to important events that take place in the device or in other applications. They notify you about certain events like battery status, network changes, or even your custom events. By using them the right way, you can make your applications smarter and more attractive.

Level up your career by joining the Full-Stack Development Bootcamp today. Gain hands-on experience with real projects and prepare for tech interviews with industry-ready Full-Stack Interview Questions.

Also, check out our other blogs related to operating systems:

Process Scheduling in Operating System CPU Scheduling in Operating System Kernel in Operating System
Thrashing in Operating System Operating System Structure Time Sharing Operating System

Broadcast Receiver in Android – FAQs

Q1. Can a Broadcast Receiver run without opening the app?

Yes, when it is registered as part of the manifest, it will also run even when the app is not in use.

Q2. Can a Broadcast Receiver handle multiple events at once?

Yes, you can add several actions to it in the IntentFilter.

Q3. Do Broadcast Receivers run on the main thread?

Yes, by default, they run on the main thread, so you should avoid heavy tasks inside them.

Q4. Can a Broadcast Receiver start an activity?

Yes, but you need to set the right flags in the intent for it to work properly.

Q5. What happens if two apps listen to the same broadcast?

Both apps will receive the broadcast unless it’s a private or ordered broadcast.

About the Author

Software Developer | Technical Research Analyst Lead | Full Stack & Cloud Systems

Ayaan Alam is a skilled Software Developer and Technical Research Analyst Lead with 2 years of professional experience in Java, Python, and C++. With expertise in full-stack development, system design, and cloud computing, he consistently delivers high-quality, scalable solutions. Known for producing accurate and insightful technical content, Ayaan contributes valuable knowledge to the developer community.

Full Stack Developer Course Banner