Android – Building a Dynamic Broadcast Receiver

1 Create the Receiver Class

First, create MyReceiver.java. This class extends BroadcastReceiver. The onReceive() method is triggered instantly by the system whenever the specified event (in this case, toggling Airplane Mode) occurs.

package com.example.android_broadcastreceivers;

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) {
        // Check if the received action is the Airplane Mode change
        if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(intent.getAction())) {
            
            // Get the current state (true = on, false = off)
            boolean isAirplaneModeOn = intent.getBooleanExtra("state", false);
            
            if (isAirplaneModeOn) {
                Toast.makeText(context, "✈️ Airplane Mode is ON", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(context, "📶 Airplane Mode is OFF", Toast.LENGTH_SHORT).show();
            }
        }
    }
}

2 Register Dynamically in Activity

In modern Android, it is best practice to register receivers dynamically in your MainActivity.java. We do this in onStart() and unregister in onStop() to prevent memory leaks when the app is backgrounded.

package com.example.android_broadcastreceivers;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {

    // Declare an instance of your receiver
    private MyReceiver airplaneModeReceiver = new MyReceiver();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        // Register the receiver with the specific filter
        IntentFilter filter = new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED);
        registerReceiver(airplaneModeReceiver, filter);
    }

    @Override
    protected void onStop() {
        super.onStop();
        // Unregister to prevent memory leaks
        unregisterReceiver(airplaneModeReceiver);
    }
}

3 Update the Layout UI

We don’t need buttons because the Android System triggers the broadcast. We just update the activity_main.xml layout with instructions for the user on how to test the app.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Swipe down from the top of your screen\nand toggle Airplane Mode to test the Receiver!"
        android:textAlignment="center"
        android:textSize="16sp"
        android:padding="20dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

App Outcome

Swipe down from the top of your screen
and toggle Airplane Mode to test the Receiver!
✈️ Airplane Mode is ON