How To Register Android BroadcastReceiver Statically

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.

  1. Dynamic register and unregister broadcast receiver is 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 is occurred in activity onCreate() method.
  4. 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.
  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 popup menu.
    create a new static android broadcast receiver in android studio
  2. 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.
    android studio new broadcast receiver panel
  3. 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();
        }
    }
  4. 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>
  5. Now when you start the android emulator, you can see a toast message popup as below picture.
    popup toast message when android device boot complete
(Visited 1,128 times, 14 visits today)
READ :   Android Drag And Drop Example

Leave a Reply

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.