package com.lila.wakeup import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import android.os.Build import android.util.Log /** * 开机自启接收器。 * * 监听 BOOT_COMPLETED / LOCKED_BOOT_COMPLETED / QUICKBOOT_POWERON 三种开机广播, * 收到后启动 [WakeupForegroundService] 进入监听状态。 * * Android 限制: * - 普通 APK 必须用户手动启动过一次 APP 后,BootReceiver 才会生效(系统安全限制)。 * - 升级到方案 B(系统签名 + /system/priv-app/)后此限制取消。 */ class BootReceiver : BroadcastReceiver() { companion object { private const val TAG = "KwsService.Boot" } override fun onReceive(context: Context, intent: Intent) { val action = intent.action Log.i(TAG, "Received boot broadcast: $action") when (action) { Intent.ACTION_BOOT_COMPLETED, Intent.ACTION_LOCKED_BOOT_COMPLETED, "android.intent.action.QUICKBOOT_POWERON" -> { startWakeupService(context) } else -> { Log.w(TAG, "Ignored unknown action: $action") } } } private fun startWakeupService(context: Context) { val serviceIntent = Intent(context, WakeupForegroundService::class.java) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { context.startForegroundService(serviceIntent) } else { context.startService(serviceIntent) } Log.i(TAG, "WakeupForegroundService started after boot") } }