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

Android 10下如何实现程序开机自启动?求通用解决方案

Android 10 开机自动启动程序的解决方案

Android 10(API 29)及以上版本对后台启动行为做了严格限制,这是导致你处理BOOT_COMPLETED事件无效果的核心原因。以下是可行的实现步骤和注意事项:

一、必要权限声明

在AndroidManifest.xml中添加权限:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<!-- Android 12+ 需额外添加通知权限 -->
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

二、配置广播接收器与前台服务

Android 10+禁止后台直接启动Activity,必须通过前台服务中转。

1. 注册组件到Manifest

<application ...>
    <!-- 开机广播接收器 -->
    <receiver
        android:name=".BootCompletedReceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.QUICKBOOT_POWERON" />
        </intent-filter>
    </receiver>

    <!-- 前台服务 -->
    <service
        android:name=".BootStartService"
        android:enabled="true"
        android:exported="true" />
</application>

2. 实现广播接收器

public class BootCompletedReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
            // 启动前台服务(Android 8+必须用startForegroundService)
            Intent serviceIntent = new Intent(context, BootStartService.class);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                context.startForegroundService(serviceIntent);
            } else {
                context.startService(serviceIntent);
            }
        }
    }
}

3. 实现前台服务

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

    @Override
    public void onCreate() {
        super.onCreate();
        // 创建通知渠道(Android 8+必填)
        createNotificationChannel();
        // 显示前台通知,否则服务会被系统杀死
        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("开机启动")
                .setContentText("程序已初始化")
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .build();
        startForeground(NOTIFICATION_ID, notification);

        // 启动目标Activity,必须添加FLAG_ACTIVITY_NEW_TASK
        Intent activityIntent = new Intent(this, MainActivity.class);
        activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(activityIntent);

        // 无需持续运行的话,可停止服务
        stopSelf();
    }

    private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
                    "开机启动通知",
                    NotificationManager.IMPORTANCE_LOW);
            NotificationManager manager = getSystemService(NotificationManager.class);
            manager.createNotificationChannel(channel);
        }
    }

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

三、关键注意事项

  • 应用必须手动启动过一次:Android 10+限制未启动过的应用接收开机广播,需用户先打开一次应用。
  • 厂商自启动权限:Pixel以外的品牌(如小米、华为)需手动开启应用的「自启动权限」,否则系统会拦截广播。
  • Android 12+通知权限:需在应用内动态申请POST_NOTIFICATIONS权限,否则前台服务无法显示通知,启动失败。示例代码:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
    if (checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
        requestPermissions(new String[]{Manifest.permission.POST_NOTIFICATIONS}, 100);
    }
}
  • 测试方式:不要用adb am broadcast模拟开机广播(系统会拦截),需真实重启手机测试。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 00:10:31