重新启动后未收到Android启动完成通知
我在应用程序清单中注册了一个广播接收器,以接收BOOT_COMPLETED通知.重新启动移动设备后,我没有收到任何通知.但是,当我打开我的应用程序时,确实会收到通知.请协助.
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 android:name=".BootCompletedReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
BootCompletedReceiver类:
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();
}
在线上有一些建议,除了BOOT_COMPLETED
操作外,您还需要某些设备支持的QUICKBOOT_POWERON
.
您可以查看问题/答案以了解详细信息.
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.
要实现此目的,我还必须添加android:enabled="false"
,然后在用户选择时按需添加,我以编程方式将其更改为android:enabled="true"
,但是尝试起来有点复杂.
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.
特殊情况:
根据设备制造商的不同,在启动时会有一些有关不同广播的报告:
Depending on device manufacturer there are some reports for different broadcasts upon boot:
- Xiaomi MIUI 使用:
android.intent.action.REBOOT
- HTC 使用:
com.htc.action.QUICKBOOT_POWERON
- Xiaomi MIUI use:
android.intent.action.REBOOT
- HTC use:
com.htc.action.QUICKBOOT_POWERON