In previous article we have learnt how to dynamically register and unregister android broadcast receiver in java code. This example will show you how to register android broadcast receiver statically in android manifest xml file.
1. Dynamic Register VS Static Register.
- Dynamic register and unregister broadcast receiver is 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 is occurred in activity onCreate() method.
- Static broadcast receiver register can resolve 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 do 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 popup menu.
- Input the broadcast receiver class name in the popup panel as below. Check Exported checkbox if you want the app to receive broadcast message from other app. Check Enabled checkbox to make the broadcast receiver enabled.
- Click Finish button, then you can see the java class is created, it also extends android.content.BroadcastReceiver. Then input below code in the onReceive() method.
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 AndroidManifest.xml file as below.Because we want this broadcast receiver listen 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 need 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 picture.
“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.