You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android开发:BootCompletedReceiver无法接收BOOT_COMPLETED广播报错

Android BOOT_COMPLETED广播接收失败解决方案
  • 【添加必要权限】在AndroidManifest.xml中添加以下权限声明,放在<application>标签外层:
<!-- 接收开机广播的必要权限 -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<!-- Android 8.0+启动前台服务所需基础权限 -->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
  • 【排除应用停止状态限制】Android 3.1及以上系统对安装后从未启动过、或者被用户手动强行停止的应用,会标记为「停止状态」,该状态下所有广播都会被拦截。你需要先手动点击应用图标启动一次应用,再测试开机广播接收效果。
  • 【国产ROM适配】小米、华为、OPPO、vivo等定制ROM默认会拦截第三方应用的开机广播,你需要进入系统设置-应用管理-当前应用-权限管理,开启「自启动」权限后再测试。
  • 【优化广播注册配置】调整<receiver>的intent-filter配置,移除不必要的配置项:
<receiver
    android:name=".BootCompletedReceiver"
    android:enabled="true"
    android:exported="true"
    android:label="boot_completed_receiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <action android:name="android.intent.action.QUICKBOOT_POWERON" />
        <!-- 不需要解锁前执行的话可删除下面这个action,它需要额外特殊权限 -->
        <!-- <action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" /> -->
    </intent-filter>
</receiver>

说明:开机广播不需要添加android.intent.category.DEFAULT分类,多余配置可能导致广播匹配失败。

  • 【修复前台服务通知渠道问题】Android 8.0及以上系统要求所有通知必须绑定专属渠道,你需要在Application的onCreate、或者BootService的onCreate方法中先创建通知渠道,再构建Notification实例:
// 在BootService的onCreate方法最前面添加这段代码
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    NotificationChannel channel = new NotificationChannel(
        "test_channel_id",
        "开机服务通知",
        NotificationManager.IMPORTANCE_LOW
    );
    NotificationManager manager = getSystemService(NotificationManager.class);
    manager.createNotificationChannel(channel);
}
  • 【优化广播接收代码】添加空值判断避免空指针崩溃:
@Override
public void onReceive(Context context, Intent intent) {
    if (intent != null && Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
        Intent i = new Intent(context.getApplicationContext(), BootService.class);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            context.startForegroundService(i);
        } else {
            context.startService(i);
        }
    }
}
  • 【额外优化建议】如果你使用的是WorkManager实现后台任务,不需要自行监听BOOT_COMPLETED广播,WorkManager默认会自动处理开机后的持久化任务恢复逻辑,可减少不必要的代码适配成本。

内容的提问来源于stack exchange,提问作者jjakubowski

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 15:27:02