2

I have a broadcast receiver registered in the application manifest to receive the BOOT_COMPLETED notification. After restarting the mobile device I receive no notification. However, I do receive the notification when I open my application. Please assist.

Receive boot completed permission from my manifest:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

Receiver from my manifest:

    <receiver android:name=".BootCompletedReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

BootCompletedReceiver class:

public class BootCompletedReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        Toast.makeText(context, "Boot Completed Received", Toast.LENGTH_LONG).show();
    }
Hendré
  • 262
  • 10
  • 27

2 Answers2

7

There are some suggestions online that except the BOOT_COMPLETED action you also need the QUICKBOOT_POWERON that is supported by some devices. You can check this Q/A for details.

Trying to implement this I also had to add the android:enabled="false" and then on demand when the user select it I programmatically changed this to android:enabled="true" but this a bit more complicated to try.

You can start by changing your code with this to see if it works.

<receiver android:name=".BootCompletedReceiver" 
    android:enabled="true">
    <intent-filter>
        <action android:name="android.intent.action.QUICKBOOT_POWERON" />
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

In case you like to try the disable logic and then enable this programmatically to do it use this code:

private static void changeBootStateReceiver(Context context, boolean enable) {
    ComponentName receiver = new ComponentName(context, BootCompletedReceiver.class);
    PackageManager pm = context.getPackageManager();

    pm.setComponentEnabledSetting(receiver,
            enable ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
                    : PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
            PackageManager.DONT_KILL_APP);
}

I also like to disable the feature when I no longer need it.

Special cases:

Depending on device manufacturer there are some reports for different broadcasts upon boot:

  • Xiaomi MIUI use: android.intent.action.REBOOT
  • HTC use: com.htc.action.QUICKBOOT_POWERON
madlymad
  • 6,367
  • 6
  • 37
  • 68
1

Check in your device that the app is allowed to run in background (sorry, am not yet able to comment).

You might also want to try LOCKED_BOOT_COMPLETED on Nougat and above. See the the docs here. It allows you to listen to actions before the user unlocks the screen.

Farouk
  • 136
  • 1
  • 10