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

Android重启后无法启动应用(已尝试全网可行方案)

Android 10+ 开机自启BroadcastReceiver无法启动Activity的解决方案

问题现象

BroadcastReceiver能正常接收ACTION_BOOT_COMPLETED广播并打印日志,但无法启动MainActivity;该功能在Android 8上正常运行,Android 10、11及更高版本失效。

原因分析

Android 10(API 29)起,系统新增后台启动Activity限制,目的是避免后台应用无差别打扰用户。即使添加Intent.FLAG_ACTIVITY_NEW_TASK标记,后台应用也无法直接启动Activity,必须满足以下条件之一:

  • 应用当前有可见窗口(如悬浮窗)
  • 用户最近与应用有交互
  • 通过通知的PendingIntent触发启动

解决方案

方案一:合规方案——通过通知引导用户启动(推荐)

修改BroadcastReceiver代码,放弃直接启动Activity,改为发送通知,用户点击通知后启动应用:

class MyReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        if (intent.action == Intent.ACTION_BOOT_COMPLETED) {
            Log.e("BootReceiver", "Boot completed. Preparing to start MainActivity.")

            // 构建启动Activity的PendingIntent
            val notificationIntent = Intent(context, MainActivity::class.java)
            val pendingIntent = PendingIntent.getActivity(
                context,
                0,
                notificationIntent,
                PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
            )

            // 创建通知
            val notification = NotificationCompat.Builder(context, "BOOT_CHANNEL")
                .setSmallIcon(R.mipmap.ic_launcher) // 替换为你的应用图标
                .setContentTitle("应用已就绪")
                .setContentText("点击启动应用")
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setContentIntent(pendingIntent)
                .setAutoCancel(true)
                .build()

            val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            // Android 8+ 必须创建通知渠道
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                val channel = NotificationChannel(
                    "BOOT_CHANNEL",
                    "开机启动通知",
                    NotificationManager.IMPORTANCE_DEFAULT
                )
                notificationManager.createNotificationChannel(channel)
            }

            // 发送通知
            notificationManager.notify(1, notification)
        }
    }
}

方案二:自动启动——需申请悬浮窗权限(不推荐,体验较差)

若必须实现开机自动启动Activity,需引导用户开启悬浮窗权限,再尝试启动:

  1. 先检查并引导用户开启悬浮窗权限:
// 在BroadcastReceiver的onReceive中添加权限检查
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(context)) {
    val overlayIntent = Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + context.packageName))
    overlayIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
    context.startActivity(overlayIntent)
    return
}

// 有权限后再启动Activity
val launchIntent = Intent(context, MainActivity::class.java)
launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(launchIntent)
  1. 注意:部分国产厂商(如小米、华为)还需用户手动开启应用的「自启动权限」,否则即使满足上述条件也无法正常启动。

额外检查

  • 确认Manifest中MainActivity的android:exported="true"配置正确(你当前的配置已满足)
  • 确认已申请RECEIVE_BOOT_COMPLETED权限(你当前的配置已满足)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 11:34:51