BroadcastReceiver Basics

suggest change

BroadcastReceivers are used to receive broadcast Intents that are sent by the Android OS, other apps, or within the same app.

Each broadcast Intent has an action string. You can also attach extras.

A BroadcastReceiver registers for intents that match an IntentFilter. Register it in code:

mContext.registerReceiver(new BroadcastReceiver() {
    @Override
   public void onReceive(Context context, Intent intent) {
      //Your implementation goes here.
   }
}, new IntentFilter("Some Action"));

or in AndroidManifest.xml:

<receiver android:name=".MyBroadcastReceiver">
    <intent-filter>
        <action android:name="Some Action" />
    </intent-filter>
</receiver>

A manifest receiver for a custom implicit action is not called when the app targets Android 8.0 (API 26) or higher. Register that receiver in code, or send the broadcast with LocalBroadcastManager.

Send the matching action with sendBroadcast:

mContext.sendBroadcast(new Intent("Some Action"));

Additionally, the Intent can contain information, such as Strings, primitives, and Parcelables, that can be viewed in onReceive.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents