In the previous article, we have learned how to dynamically register and unregister the android broadcast receivers in java code. This example will show you how to register the android broadcast receiver statically in the android manifest XML file.
1. Dynamic Register VS Static Register.
- Dynamic register and unregister broadcast receiver are very flexible.
- You can dynamically register all broadcast intent actions in java code.
- But when dynamically register, it can receive the broadcast message only after the android app startup, because the register broadcast receiver action occurs in the activity onCreate() method.
- A static broadcast receiver register can resolve the above issue. When you register a broadcast receiver in static mode, the android OS will create a new process to handle the broadcast even the app does not startup.
- But static register can not handle all broadcast action events.
2. How To Create Static Register Android Broadcast Receiver.
- Right-click the package name, click New —> Other —> Broadcast Receiver in the popup menu.
- Input the broadcast receiver’s Class Name in the popup wizard panel. Check the Exported checkbox if you want the app to receive a broadcast message from another app. Check the Enabled checkbox to make the broadcast receiver enabled.
- Click the Finish button, then you can see the java class is created, it also extends android.content.BroadcastReceiver. Then input the below code in the onReceive() method.
- Below is the source code of the created java file BootCompleteReceiver.java.
package com.dev2qa.example.broadcast.receiver; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; public class BootCompleteReceiver extends BroadcastReceiver { // When receive BOOT_COMPLETE broadcast, show a toast popup. @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "Android device boot complete.", Toast.LENGTH_LONG).show(); } }
- After that, you can see a receiver XML tag has been added in the AndroidManifest.xml file as below. Because we want this broadcast receiver to listen to the android device boot complete event, so we add an intent-filter with action android.intent.action.BOOT_COMPLETED.
- To listen above action event, this app needs to add android.permission.RECEIVE_BOOT_COMPLETED permission in AndroidManifest.xml file also.
- AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.dev2qa.example"> <!-- Need to add below permission to receive boot complete broadcast message.--> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <receiver android:name=".broadcast.receiver.BootCompleteReceiver" android:enabled="true" android:exported="true"> <!--Configured this broadcast receiver to receive boot complete broadcast event.--> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> </application> </manifest>
- Now when you start the android emulator, you can see a toast message popup as below video.
If you can not watch the above video, you can see it on the youtube URL https://youtu.be/Ol2DgpiB2PM
Great insights on registering Android BroadcastReceivers statically! I appreciate the clear explanations and the tips for Windows and iPhone tricks. This will definitely help streamline my app development process. Thanks for sharing!
“When you register a broadcast receiver in static mode, the android OS will create a new process to handle the broadcast even the app do not startup.”
The Broadcast Receiver is registered during the first app start, so your above comment is in-correct for first app start scenario.