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

Android 15下ACTION_BOOT_COMPLETED失效,如何可靠检测真实开机?

背景

在Android 14及之前版本中,使用ACTION_BOOT_COMPLETED可正常检测系统开机并执行相关操作,示例实现如下:

BootReceiver.kt

class BootReceiver : BroadcastReceiver() {

    @SuppressLint("UnsafeProtectedBroadcastReceiver")
    @UiThread
    override fun onReceive(context: Context, intent: Intent) {
             ...

清单文件配置

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

...

<receiver
    android:name=".receivers.BootReceiver" android:enabled="false" android:exported="false">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />

        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</receiver>

问题

在Android 15中,该Intent会在多种非开机场景下触发,例如Android Studio调试、强制停止应用后从应用信息页启动等。

已尝试方案

计划使用API 34(Android 14及以上支持)的addApplicationStartInfoCompletionListener,通过ApplicationStartInfo.getReason()判断启动原因,若返回值为START_REASON_BOOT_COMPLETE则判定为真实开机,示例代码如下:

inline fun <reified T : Any> Context.getSystemServiceCompat(): T =
        ContextCompat.getSystemService(applicationContext, T::class.java)!!

...

val activityManager: ActivityManager = context.getSystemServiceCompat()
activityManager.addApplicationStartInfoCompletionListener(ExecutorEx.diskIO) { applicationStartInfo ->
            val reasonStr = when (val reason = applicationStartInfo.reason) {
                ApplicationStartInfo.START_REASON_ALARM -> "START_REASON_ALARM"
                ApplicationStartInfo.START_REASON_BACKUP -> "START_REASON_BACKUP"
                ApplicationStartInfo.START_REASON_BOOT_COMPLETE -> "START_REASON_BOOT_COMPLETE"
                ApplicationStartInfo.START_REASON_BROADCAST -> "START_REASON_BROADCAST"
                ApplicationStartInfo.START_REASON_CONTENT_PROVIDER -> "START_REASON_CONTENT_PROVIDER"
                ApplicationStartInfo.START_REASON_JOB -> "START_REASON_JOB"
                ApplicationStartInfo.START_REASON_LAUNCHER -> "START_REASON_LAUNCHER"
                ApplicationStartInfo.START_REASON_LAUNCHER_RECENTS -> "START_REASON_LAUNCHER_RECENTS"
                ApplicationStartInfo.START_REASON_OTHER -> "START_REASON_OTHER"
                ApplicationStartInfo.START_REASON_PUSH -> "START_REASON_PUSH"
                ApplicationStartInfo.START_REASON_SERVICE -> "START_REASON_SERVICE"
                ApplicationStartInfo.START_REASON_START_ACTIVITY -> "START_REASON_START_ACTIVITY"
                else -> {
                    logException("unknown applicationStartInfo reason:$reason")
                    "Unknown:$reason"
                }
            }
            Log.d("AppLog", "reasonStr:$reasonStr")

但实际开机时返回的是START_REASON_BROADCAST,且该方法为回调形式,无法直接在BootReceiver的onReceive方法中使用。

同时发现真实开机与非开机场景(假开机)的Intent Flag不同:

  • 非开机场景(仅启动应用):Flag为0x9000030
  • 真实开机:Flag为0x89000010

疑问

  1. 在Android 15中,是否存在可靠方法可在已创建的BootReceiver类中检测真实开机?
  2. 通过检测Intent Flag的方式是否属于可靠的检测方案?

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 03:22:06