Android Broadcast Receiver Example

Simple-Android-Broadcast-Receiver-Example.png

5th February, 2023

Hello Android developer! In this tutorial, we will learn how to create a simple Android Broadcast Receiver in Android.

From Android 8.0 or higher, You cannot use static Android broadcast receiver for most implicit broadcasts. We are using dynamic Android broadcast receiver in this example.

Step 1: Create New Project

Create a new project in Android Studio from File ⇒ New Project and select Empty Activity from the templates.

Step 2: Create BroadcastReceiver In Android Studio

Open the project tool window by clicking on the project option from the Tool window bar. After that select the Android option from the drop-down and open the app → java hierarchy.

Right click on the package name and select New → Other → Broadcast Receiver.

Create-New-Android-Broadcast-Receiver-In-Android-Studio.png

Fill the required details and click on finish button.

Create-New-Android-Broadcast-Receiver-In-Android-Studio-Fill-details.png

Alternatively you can create a subclass of Android’s BroadcastReceiver class. Override the onReceive() method.

content_copy light_mode remove
package com.androidchunk.broadcastreceivertest

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.provider.Settings
import android.widget.Toast

class MyReceiver : BroadcastReceiver() {

    // This method is called
    // when the BroadcastReceiver is receiving an Intent broadcast.
    override fun onReceive(context: Context, intent: Intent) {

        // Filter the intent action, We need only Airplane mode change action,
        // We don't want other at this time!
        if (intent.action == Intent.ACTION_AIRPLANE_MODE_CHANGED) {

            //get latest airplane mode value from the settings
            val airplaneModeInt =
                Settings.System.getInt(context.contentResolver, Settings.Global.AIRPLANE_MODE_ON, 0)

            //check Airplane mode is on or off
            if (airplaneModeInt == 0) {
                Toast.makeText(context, "Airplane mode turned off", Toast.LENGTH_SHORT).show()
            } else {
                Toast.makeText(context, "Airplane mode turned on", Toast.LENGTH_SHORT).show()
            }
        }
    }
}
package com.androidchunk.broadcastreceivertest;

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

class MyReceiver extends BroadcastReceiver {

    // This method is called
    // when the BroadcastReceiver is receiving an Intent broadcast.
    @Override
    public void onReceive(Context context, Intent intent) {
        // Filter the intent action, We need only Airplane mode change action,
        // We don't want other at this time!
        if (intent.getAction().equals(Intent.ACTION_AIRPLANE_MODE_CHANGED)) {

            //get latest airplane mode value from the settings
            int airplaneModeInt =
                    Settings.System.getInt(context.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 0);

            //check Airplane mode is on or off
            if (airplaneModeInt == 0) {
                Toast.makeText(context, "Airplane mode turned off", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(context, "Airplane mode turned on", Toast.LENGTH_SHORT).show();
            }
        }
    }
}

Step 3: Register Android BroadcastReceiver in the context

Open the activity and create an instance of the MyReceiver class that we created in the previous step. Create another instance of Android’s IntentFilter class with Intent.ACTION_AIRPLANE_MODE_CHANGED as the constructor parameter. Using this parameter we will able to receive the broadcast when the airplane mode of the deivce changes.

content_copy light_mode remove
package com.androidchunk.broadcastreceivertest

import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

    //create an object of BroadcastReceiver
    private var broadcastReceiver = MyReceiver()

    //create an object of IntentFilter
    private var intentFilter = IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

    override fun onStart() {
        super.onStart()
        //register broadcast receiver
        registerReceiver(broadcastReceiver, intentFilter)
    }

    override fun onDestroy() {
        super.onDestroy()
        //unregister broadcast receiver
        unregisterReceiver(broadcastReceiver)
    }
}
package com.androidchunk.broadcastreceivertest;

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

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

class MainActivity extends AppCompatActivity {

    //create an object of BroadcastReceiver
    private final BroadcastReceiver broadcastReceiver = new MyReceiver();

    //create an object of IntentFilter
    private final IntentFilter intentFilter = new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED);

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onStart() {
        super.onStart();
        //register broadcast receiver
        registerReceiver(broadcastReceiver, intentFilter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //unregister broadcast receiver
        unregisterReceiver(broadcastReceiver);
    }
}

Output

Happy coding!

Leave a Reply