Android boot completed notification not received after reboot

谁都会走 提交于 2019-12-23 04:54:32

问题


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();
    }

回答1:


There as 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.

Tring 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



回答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.



来源:https://stackoverflow.com/questions/53847965/android-boot-completed-notification-not-received-after-reboot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!