How To Register Android BroadcastReceiver Statically

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.

  1. Dynamic register and unregister broadcast receiver are very flexible.
  2. You can dynamically register all broadcast intent actions in java code.
  3. 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.
  4. 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.
  5. But static register can not handle all broadcast action events.

2. How To Create Static Register Android Broadcast Receiver.

  1. Right-click the package name, click New —> Other —> Broadcast Receiver in the popup menu.
  2. 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.
  3. 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.
  4. 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();
        }
    }
  5. 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.
  6. To listen above action event, this app needs to add android.permission.RECEIVE_BOOT_COMPLETED permission in AndroidManifest.xml file also.
  7. 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>
  8. 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

3 thoughts on “How To Register Android BroadcastReceiver Statically”

  1. Great insights on registering BroadcastReceivers! The step-by-step process you outlined for static registration really clarifies things. I appreciate the Windows tricks as well—very helpful for cross-platform developers. Looking forward to more posts like this!

  2. 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!

  3. “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.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.