View video tutorial

ANDROID Broadcast Receiver

ANDROID

The Android broadcast receiver is a component that allows an app to respond to system-wide events or custom events from other apps.

Android Broadcast Receiver


➔ Broadcast receivers simply respond to broadcast messages from other applications or systems, and these messages are sometimes called events or intents.

➔ It remains inactive until a specific event (an intent) occurs; for example, it only sends messages when an event occurs, such as a "battery low" alert or a "airplane mode" change.

➔ A broadcast receiver does not require any visual components or screens, and it typically triggers other components without requiring any visual operations, such as starting a service or displaying a notification.

➔ The receiver object is only valid for the duration of the onReceive() method call, and after the method returns, the system considers the component inactive and may terminate the process.

➔ By default, onReceive() runs on the app's main thread. To avoid "Application Not Responding" (ANR) errors, long-running tasks should be delegated to a service or background worker, which relieves the pressure on the main thread by starting a separate new thread.

Purpose of Broadcast Receiver


The purpose of a broadcast receiver is to respond to system events such as battery level changes, network connectivity updates, or custom app notifications. This does not require the app to be constantly running and monitoring; rather, the app uses the broadcast receiver to receive its response as soon as the event occurs.

Creating Broadcast Receiver


A class that extends the BroadcastReceiver class can also be considered a BroadcastReceiver class. This subclass can now override the onReceive(Context, Intent) method, and this method contains the actual logic to be executed when the event is detected.

Broadcast Receiver register

Android broadcast receivers can be registered in two main ways: statically via the AndroidManifest.xml file (which works even when the app is closed) or dynamically via code (within an activity/service, which is limited to the app's lifecycle). Static registration is used for system-level events, while dynamic registration is for app-specific events.


Static Registration (Manifest-declared)

syntax

<receiver android:name=".MyCustomBroadcastReceiver" android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.POWER_CONNECTED"/>
    </intent-filter>
</receiver>

This example uses static registration in the AndroidManifest.xml file and registers the MyCustomBroadcastReceiver receiver class for the system-level event ACTION_POWER_CONNECTED. This broadcast receiver will continue to run even if the app is closed or not running. The Android system registers BroadcastReceiver during installation, allowing it to start the app process and automatically deliver broadcasts.

Dynamic Registration (Context-registered)

syntax

IntentFilter filter = new IntentFilter(Intent.ACTION_POWER_CONNECTED);
MyCustomBroadcastReceiver receiver = new MyCustomBroadcastReceiver();
registerReceiver(receiver, filter);
// Unregister in onPause() or onDestroy() callback methods.
unregisterReceiver(receiver);

This example registers a broadcast receiver named MyCustomBroadcastReceiver and this registration can be done using the registerReceiver() method inside an activity or service.

This broadcast receiver will not work if the app is closed. It only receives broadcasts when the registering context (such as an activity or service) is active and updates UI elements only when the app is in the foreground.

System level broadcast events


Several system level events are defined as final static fields of the Intent class. Common system-level broadcast intents used in Android development. See the Android Online official documentation for the full action list.

Connectivity & Hardware Action

android.intent.action.AIRPLANE_MODE: It is activated when Airplane mode is turned on or off.

android.net.conn.CONNECTIVITY_CHANGE: The network connectivity action is triggered when the WiFi, network, or mobile data connection changes.

android.bluetooth.adapter.action.STATE_CHANGED: The Bluetooth State action is triggered when Bluetooth is turned on or off.

android.intent.action.HEADSET_PLUG: The Headset Status action is triggered when a wired headset is plugged in or out.

Power & Battery Action

android.intent.action.BATTERY_LOW: The low battery action is activated when the battery charge is extremely low.

android.intent.action.ACTION_POWER_CONNECTED: The power connection function is activated when the device starts charging.

android.intent.action.ACTION_POWER_DISCONNECTED: The power disconnection process is activated when the device stops charging.

System Lifecycle Action

android.intent.action.BOOT_COMPLETED: The 'Boot Completed' action is triggered when the system finishes booting, but this requires the RECEIVE_BOOT_COMPLETED permission.

android.intent.action.SCREEN_ON and android.intent.action.SCREEN_OFF: The Screen Status action is triggered when the screen is turned on or off.

android.intent.action.ACTION_SHUTDOWN: The shutdown action is triggered when the device is turned off.

Date & Time Action

android.intent.action.TIME_SET: When the user sets the time themselves, the 'Time Changed' action is triggered.

android.intent.action.TIMEZONE_CHANGED: The 'Timezone Changed' action is triggered when the system's timezone changes.

android.intent.action.DATE_CHANGED: The 'Change Date' action is triggered at midnight when the date changes.

Application & Package Management Action

android.intent.action.PACKAGE_ADDED: When a new application is installed, the 'Package Added' action is triggered.

android.intent.action.PACKAGE_REMOVED: When an application is uninstalled, the 'Package Removed' action is triggered.

android.intent.action.MY_PACKAGE_REPLACED: When an app is updated to a new version, my 'package replaced' action is triggered.