You need to enable JavaScript to run this app. Android SDK29及以上版本设备开机启动服务失败如何解决?
优惠活动
大模型
产品
解决方案
定价
更多

Android SDK29及以上版本设备开机启动服务失败如何解决?

安卓高版本(SDK29/30)开机启动服务失败解决方案

核心问题及修复步骤

  • 1 修复广播接收器的exported配置
    你当前把BootReceiver的android:exported设为了false,这会导致系统发出的BOOT_COMPLETED广播无法被你的应用接收,高版本安卓对组件导出的校验更加严格,必须修改为true。
  • 2 补充前台服务必需权限
    SDK28(Android 9)及以上版本启动前台服务需要申请普通权限android.permission.FOREGROUND_SERVICE,否则启动会直接报错。如果适配到SDK31(Android 12)及以上,还需要根据服务类型补充对应前台权限,通用场景可补充android.permission.FOREGROUND_SERVICE_SPECIAL_USE。
  • 3 修正广播中Intent参数传递的错误
    你当前代码中是给广播接收的intent对象添加Extra,而非给要启动服务的service对象添加,会导致服务无法获取到正确的参数,逻辑出错后自动退出。
  • 4 服务内部必须在5秒内调用startForeground()
    调用startForegroundService()之后,系统要求服务必须在5秒内调用startForeground()展示常驻通知,否则服务会被系统强制杀死,触发ANR错误。
  • 5 适配厂商定制系统限制
    小米、华为、vivo、OPPO等国内厂商的系统都有独立的自启动权限管控,需要引导用户手动在系统设置中开启应用的「自启动」权限,否则无法接收到开机广播。
  • 6 可选:提高开机广播的接收优先级
    给BOOT_COMPLETED的intent-filter添加优先级android:priority="1000",提高广播被系统优先分发的概率。

修正后的代码示例

清单文件配置

<!-- 补充前台服务权限 -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<!-- 适配Android12及以上可加下面这条 -->
<!-- <uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" /> -->

<receiver 
    android:name=".awc.BootReceiver" 
    android:exported="true" 
    android:enabled="true">
    <intent-filter android:priority="1000">
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

<service 
    android:name=".awc.AWC_Service"
    android:exported="true"
    android:enabled="true"
    android:process=":ServiceProcess"
    <!-- 适配Android12及以上需要指定前台服务类型 -->
    <!-- android:foregroundServiceType="specialUse" -->
     />

广播接收器代码修正

public class BootReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        PrefManager prefManager = new PrefManager(context.getApplicationContext());
        if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
            if (!prefManager.getBoolean("AWC_Disabled")) {
                Intent service = new Intent(context.getApplicationContext(), AWC_Service.class);
                // 修正:给启动服务的Intent加参数
                service.putExtra("StopSer", false);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    context.startForegroundService(service);
                } else {
                    context.startService(service);
                }
                Log.e("Phone Restart", "AWS Service start");
            }
            Log.e("Phone Restart", "AWS Service start1");
        }
    }
}

服务端补充startForeground逻辑示例

public class AWC_Service extends Service {
    private static final int NOTIFICATION_ID = 1001;
    private static final String CHANNEL_ID = "AWC_SERVICE_CHANNEL";

    @Override
    public void onCreate() {
        super.onCreate();
        // 安卓8.0及以上需要先创建通知渠道
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "AWC服务", NotificationManager.IMPORTANCE_LOW);
            NotificationManager manager = getSystemService(NotificationManager.class);
            manager.createNotificationChannel(channel);
        }
        // 构建通知
        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("AWC服务正在运行")
                .setContentText("服务后台运行中")
                .setSmallIcon(R.drawable.ic_launcher) // 替换为你的应用图标
                .build();
        // 5秒内必须调用startForeground
        startForeground(NOTIFICATION_ID, notification);
    }

    // 其余服务逻辑...
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.14 17:15:03