Android Broadcast Receiver

In this tutorial, you will get a complete understanding of the Android Broadcast Receiver. You will learn about system-generated intents with their working, classes of broadcasts and implementation of broadcast receivers in Android studio.

What is Android Broadcast Receiver?

A broadcast receiver is an Android component that allows an application to respond to messages (an Android Intent) that are broadcast by the Android operating system or by an application. To deliver events to the app like sending a low battery message or screen turned off the message to the app. The apps can also initiate broadcasts to let other apps know that required data available in a device to use it.

Generally, we use Intents to deliver broadcast events to other apps and Broadcast Receivers use status bar notifications to let the user know that broadcast event occurs.

In android, Broadcast Receiver is implemented as a subclass of BroadcastReceiver and each broadcast is delivered as an Intent object.

Receiving Broadcasts

In android, we can receive broadcasts by registering in two ways.

One way is by registering broadcasts using an android application manifest file (AndroidManifest.xml). We need to specify <receiver> element in apps manifest file like as shown below.

<receiver android:name=".SampleBroadcastReceiver">

        <action android:name="android.intent.action.BOOT_COMPLETED"/>

The above statement will fire the defined system broadcast event whenever the boot process is completed.

Another way is to register a receiver dynamically via Context.registerReceiver() method. To register broadcast receiver we need to extend our class using BroadcastReceiver and need to implement an onReceive(Context, Intent) method like as shown below.

public class MainActivity extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, log, Toast.LENGTH_LONG).show();

Suppose if we register for ACTION_BOOT_COMPLETED event, whenever the boot process is completed the broadcast receiver’s method onReceive() method will be invoked.

Sending Broadcasts

In android, we can send a broadcasts in apps using three different ways, those are

sendOrderedBroadcast(Intent, String)This method is used to send broadcasts to one receiver at a time.
sendBroadcast(Intent)This method is used to send broadcasts to all receivers in an undefined order.
LoadBroadcastManager.sendBroadcastThis method is used to send broadcasts to receivers that are in the same app as the sender.

Broadcasting Custom Intents

In android, we can create our own custom broadcasts using intents. Following is the simple code snippet of sending a broadcast by creating an intent using sendBroadcast(Intent) method.

Intent sintent = new Intent();
intent.putExtra("data","Welcome to Codingtimes");

If you observe above code snippet we create a custom Intent “sintent”. We need to register our intent action in android manifest file like as shown below

<receiver android:name=".SampleBroadcastReceiver">

        <action android:name="in.codingtimes.broadcast.MY_NOTIFICATION"/>

This is how we can create our own custom brDescriptionDescriptionoadcasts using Intents in android applications.

System Broadcasts

In android, several system events are defined as final static fields in the Intent class. The following are some of the system events available in android applications.

android.intent.action.BOOT_COMPLETEDIt raises an event, once boot completed.
android.intent.action.POWER_CONNECTEDIt is used to trigger an event when power connected to the device.
android.intent.action.POWER_DISCONNECTEDIt is used to trigger an event when the power got disconnected from the device.
android.intent.action.BATTERY_LOWIt is used to call an event when battery is low on device.
android.intent.action.BATTERY_OKAYIt is used to call an event when a battery is OKAY again.
android.intent.action.REBOOTIt call an event when the device rebooted again.

Likewise, we have many system events available in the android application.

Android BroadcastReceiver Example

To setup a Broadcast Receiver in our android application we need to do the following things.

  • Create a BroadcastReceiver
  • Register a BroadcastReceiver

Create a new android application using android studio and give names as BroadcastReceiver.

Now we need to create our own broadcast content file in \src\main\java\in.codingtimes.broadcastreceiver path to define our actual provider and associated methods for that right-click on your application folder à Go to New à select Java Class and give name as

Once we create a new file, open it and write the code like as shown below

package in.codingtimes.broadcastreceiver_android;

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

public class BroadcastReceived extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Broadcast Received", Toast.LENGTH_SHORT).show();

Now open activity_main.xml file from \src\main\res\layout path and write the following code.


<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android=""

        app:layout_constraintTop_toTopOf="parent" />

Now open file from \java\in.codingtimes.broadcastreceiver path and write following to implement custom broadcast intents.

package in.codingtimes.broadcastreceiver_android;


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

public class MainActivity extends AppCompatActivity {

    BroadcastReceiver br = new BroadcastReceived();

    protected void onCreate(Bundle savedInstanceState) {
        Button b = findViewById(;
        b.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent intent = new Intent();
                Toast.makeText(MainActivity.this, "Broadcast Sent", Toast.LENGTH_SHORT).show();

    protected void onStart() {
        IntentFilter intentFilter = new IntentFilter("mybroadcast");

    protected void onStop() {

Now we need to register our broadcast receiver in android manifest file (AndroidManifest.xml) using <receiver> attribute like as shown below.


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            <receiver android:name=".BroadcastReceived"
                    <category android:name="android.intent.category.DEFAULT"/>



That’s it!
You have successfully completed the post. Do Share : )

Peace Out!

Source Code –

Also Read – Android Fragment Life Cycle

Check Out Deals on -> Amazon , Flipkart , Myntra , Adidas , Apple TV , Boat , Canva , Beardo , Coursera , Cleartrip , Fiverr , MamaEarth , SwiggyKFC

One comment

Leave a Reply

Your email address will not be published.