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

锁屏时能否启动后台应用?如何通过broadcast receiver唤醒解锁屏幕?

Android 屏幕关闭时启动后台应用相关问题解答

疑问1:屏幕关闭时是否可以启动后台应用?具体操作方式是什么?

可以实现,但受Android版本和系统权限管控,核心操作步骤如下:

  • 申请必要权限:在AndroidManifest.xml中添加权限声明
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
    <!-- Android 10以下可添加,高版本用FLAG_DISMISS_KEYGUARD替代 -->
    <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
    
  • 获取唤醒锁:启动应用前,通过PowerManager获取唤醒锁,确保设备保持唤醒状态
    val powerManager = getSystemService(Context.POWER_SERVICE) as PowerManager
    val wakeLock = powerManager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK or PowerManager.ACQUIRE_CAUSES_WAKEUP, "App:WakeLockTag")
    wakeLock.acquire(10*1000) // 设定超时时间,避免长期持有
    
  • 启动目标Activity:给Intent添加必要标记,并在Activity中配置解锁唤醒逻辑
    val intent = Intent(this, TargetActivity::class.java)
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
    startActivity(intent)
    
    在TargetActivity的onCreate方法中添加窗口标记:
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        window.addFlags(
            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
            or WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
            or WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
        )
        // Android 12+ 推荐使用API替代
        // setShowWhenLocked(true)
        // setTurnScreenOn(true)
    }
    

疑问2:当broadcast receiver被激活且屏幕处于关闭状态时,能否启动后台应用?当前未运行任何后台service,仅希望唤醒并解锁屏幕,显示后台应用中的activity;目前仅能在应用处于foreground时锁屏的情况下实现唤醒解锁操作。

可以实现,但需要规避广播限制并完善唤醒逻辑:

  • 广播类型选择:Android 8.0+对静态注册的隐式广播限制严格,建议使用动态注册广播;若为系统广播,确保其属于允许静态注册的白名单范畴(如ACTION_BOOT_COMPLETED)。
  • 广播接收器中的处理逻辑:不能直接在接收器中启动Activity,需先获取唤醒锁再启动:
    class MyBroadcastReceiver : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent?) {
            context?.let { ctx ->
                // 获取唤醒锁
                val powerManager = ctx.getSystemService(Context.POWER_SERVICE) as PowerManager
                val wakeLock = powerManager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK or PowerManager.ACQUIRE_CAUSES_WAKEUP, "App:ReceiverWakeLock")
                wakeLock.acquire(10*1000)
    
                // 启动目标Activity
                val activityIntent = Intent(ctx, TargetActivity::class.java)
                activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                ctx.startActivity(activityIntent)
            }
        }
    }
    
  • 适配高版本Android(10+):后台启动Activity必须使用全屏Intent,需配合通知发送:
    // 在广播接收器中创建全屏Intent
    val fullScreenIntent = Intent(context, TargetActivity::class.java)
    val fullScreenPendingIntent = PendingIntent.getActivity(context, 0, fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
    
    val notification = NotificationCompat.Builder(context, "CHANNEL_ID")
        .setSmallIcon(R.drawable.ic_notification)
        .setContentTitle("唤醒通知")
        .setContentText("即将启动应用")
        .setPriority(NotificationCompat.PRIORITY_HIGH)
        .setCategory(NotificationCompat.CATEGORY_ALARM)
        .setFullScreenIntent(fullScreenPendingIntent, true)
        .build()
    
    val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    notificationManager.notify(1, notification)
    
  • 确保目标Activity的唤醒解锁逻辑和疑问1中的一致,同时注意:若应用从未启动过,部分厂商系统可能会限制后台启动权限,需引导用户手动开启。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 06:16:08