Android boot completed notification not received after reboot
Asked Answered
S

2

3

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();
    }
Squeaky answered 19/12, 2018 at 9:16 Comment(0)
A
9

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
Alvarez answered 19/12, 2018 at 9:50 Comment(1)
I have tried your solution but I am still not receiving this. I tried in OnePlus mobileMiru
M
2

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.

Mclaughlin answered 19/12, 2018 at 9:23 Comment(1)
@madlymad, where does the second piece of code go?Thai

© 2022 - 2024 — McMap. All rights reserved.