BroadcastReceiver Basics
suggest changeBroadcastReceivers 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.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents