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

Android API 30下实现开机自启动应用遇阻,现有代码无效求助

Android API 30 开机启动应用失败的解决方案

Android 11(API 30)强化了后台启动Activity的限制:当系统发送BOOT_COMPLETED广播时,应用处于后台状态,直接调用startActivity()会被系统拦截,这是导致你代码失效的核心原因。以下是合规的解决方案:


1. 改用前台服务过渡启动Activity

后台直接启动Activity被禁止,我们可以先启动前台服务(系统允许后台启动前台服务),再由服务触发Activity启动,同时需适配通知渠道:

修改广播接收器代码

public class StartMyServiceAtBootReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(final Context context, Intent intent) {
        if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())){
            Intent serviceIntent = new Intent(context, BootStartService.class);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                context.startForegroundService(serviceIntent);
            } else {
                context.startService(serviceIntent);
            }
        }
    }
}

创建前台服务类 BootStartService

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // 创建适配Android 8.0+的通知渠道
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "开机启动通知", NotificationManager.IMPORTANCE_LOW);
            NotificationManager manager = getSystemService(NotificationManager.class);
            if (manager != null) {
                manager.createNotificationChannel(channel);
            }
        }

        // 构建前台通知
        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setContentTitle("应用启动中")
                .setContentText("正在打开应用")
                .setPriority(NotificationCompat.PRIORITY_LOW)
                .build();

        // 启动前台服务
        startForeground(NOTIFICATION_ID, notification);

        // 延迟1秒启动Activity,确保通知已完成显示
        new Handler(Looper.getMainLooper()).postDelayed(() -> {
            Intent activityIntent = new Intent(this, MainActivity.class);
            activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(activityIntent);
            // 启动完成后停止服务
            stopSelf();
        }, 1000);

        return START_NOT_STICKY;
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

在Manifest中注册服务

<service
    android:name=".BootStartService"
    android:foregroundServiceType="specialUse" />

2. 满足必要前提条件

  • 应用必须被用户手动启动过一次:Android 10及以上系统中,未被用户手动打开过的应用,会被系统忽略BOOT_COMPLETED广播,测试前务必先手动启动一次应用。
  • 关闭电池优化:部分厂商的电池优化策略会阻止开机启动,可引导用户将应用加入白名单:
    PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
    if (!pm.isIgnoringBatteryOptimizations(getPackageName())) {
        Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
        intent.setData(Uri.parse("package:" + getPackageName()));
        startActivity(intent);
    }
    
    同时在Manifest中添加权限:
    <uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 02:20:28