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

Android 14设备全屏Intent无法触发问题求助

Android 14/15 锁屏下全屏Intent不触发问题解决

问题核心

运行Android 14的Pixel 8 Pro(升级Android 15 Beta后依旧),锁屏状态下闹钟通知无法触发全屏Intent,仅显示通知;解锁后重新锁屏,若通知存在则可正常触发。Android 13设备(三星A51)无此问题。已配置USE_FULL_SCREEN_INTENT等权限,尝试过唤醒锁、窗口flags等常规方案无效。

针对性解决方案

1. 在发送通知前强制唤醒设备(关键)

Android 14+对深度休眠(Doze)状态下的设备唤醒限制更严格,需在发送通知前主动唤醒设备,避免因设备休眠导致全屏Intent无法触发:

// 在BroadcastReceiver中发送通知前执行
val powerManager = context.getSystemService(Context.POWER_SERVICE) as PowerManager
val wakeLock = powerManager.newWakeLock(
    PowerManager.SCREEN_DIM_WAKE_LOCK or 
    PowerManager.ACQUIRE_CAUSES_WAKEUP or 
    PowerManager.ON_AFTER_RELEASE,
    "AlarmApp:AlarmWakeLock"
)
// 按需设置超时时间,比如10分钟
wakeLock.acquire(10 * 60 * 1000L)

注意:SCREEN_BRIGHT_WAKE_LOCK已在Android 12+弃用,改用SCREEN_DIM_WAKE_LOCK;ACQUIRE_CAUSES_WAKEUP会强制唤醒休眠的设备,ON_AFTER_RELEASE确保屏幕在唤醒锁释放后保持点亮一段时间。

2. 确保通知渠道配置符合高优先级要求

Android 14+对通知渠道的重要性校验更严格,创建渠道时必须明确设置IMPORTANCE_HIGH:

val mChannel = NotificationChannel(
    "alarm_channel",
    "闹钟提醒",
    NotificationManager.IMPORTANCE_HIGH
).apply {
    description = "应用闹钟通知"
    lockscreenVisibility = Notification.VISIBILITY_PUBLIC
    // 若需要自定义闹钟铃声,可设置setSound()
}
val notificationManager = context.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(mChannel)

3. 优化PendingIntent与Activity配置

  • 调整PendingIntent的flags,避免因复用导致的异常:
val fullScreenPendingIntent = PendingIntent.getActivity(
    context,
    System.currentTimeMillis().toInt(), // 用时间戳作为唯一requestCode,避免复用
    alarmIntent,
    PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT,
    options.toBundle()
)
  • 在Activity的onCreate中立即设置窗口flags(即使使用Jetpack Compose):
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    // 强制设置窗口在锁屏时显示、唤醒屏幕并保持点亮
    window.setFlags(
        WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED or
                WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON or
                WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
        WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED or
                WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON or
                WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
    )
    // 释放之前获取的唤醒锁
    val powerManager = getSystemService(Context.POWER_SERVICE) as PowerManager
    val wakeLock = powerManager.newWakeLock(
        PowerManager.SCREEN_DIM_WAKE_LOCK or PowerManager.ON_AFTER_RELEASE,
        "AlarmApp:AlarmWakeLock"
    )
    if (wakeLock.isHeld) wakeLock.release()

    setContent {
        // Compose UI内容
    }
}

4. 关闭Pixel设备的特殊电池优化

Pixel设备的自适应电池和应用休眠可能拦截唤醒操作:

  • 进入「设置」→「应用」→「你的闹钟应用」→「电池」
  • 开启「不受电池优化限制」(选择「允许」)
  • 关闭「应用休眠」选项

5. 尝试通过前台服务发送通知

若上述方案无效,可在BroadcastReceiver中启动前台服务,由服务发送通知(前台服务优先级更高,更易绕过休眠限制):

// BroadcastReceiver中启动前台服务
val serviceIntent = Intent(context, AlarmForegroundService::class.java)
ContextCompat.startForegroundService(context, serviceIntent)

在AlarmForegroundService的onStartCommand中发送通知,并调用startForeground():

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
    // 这里执行发送通知的代码(同之前的逻辑)
    startForeground(1, builder.build())
    return START_STICKY
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 10:05:24