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

开启Extend Unlock/Smart Lock时Full Screen Intent失效问题求助

VOIP通话应用全屏通知在Extend Unlock/Smart Lock开启时失效的解决方案

问题现象

开发的VOIP通话应用中,全屏通知(Full Screen Notification)在多数场景下可正常触发,但当设备开启Extend Unlock或Smart Lock设置时,有时无法弹出全屏通话界面,仅在通知栏显示通话推送;关闭Extend Unlock后功能恢复正常。已配置Manifest所需权限及Activity属性,且在NewCallActivity的onCreate方法中实现了唤醒屏幕与解锁逻辑。

已配置的Manifest关键代码:

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

<activity
    android:name=".ui.puurcall.ringingcall.NewCallActivity"
    android:configChanges="fontScale|uiMode"
    android:exported="true"
    android:launchMode="singleTop"
    android:screenOrientation="portrait"
    android:showOnLockScreen="true"
    android:theme="@style/Theme.PUUR"
    android:turnScreenOn="true" />

NewCallActivity的唤醒解锁逻辑:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(view)
    turnScreenOnAndKeyguardOff()
}

fun Activity.turnScreenOnAndKeyguardOff() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
        setShowWhenLocked(true)
        setTurnScreenOn(true)
    } else {
        window.addFlags(
            WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON or WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON
        )
    }

    with(getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            requestDismissKeyguard(this@turnScreenOnAndKeyguardOff, null)
        }
    }
}

可能的解决方案

1. 检查全屏通知的构建逻辑

确保创建Notification时,正确配置fullScreenIntent,同时将通知优先级设为最高:

val fullScreenIntent = Intent(this, NewCallActivity::class.java)
val pendingIntent = PendingIntent.getActivity(
    this,
    0,
    fullScreenIntent,
    PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)

val notificationBuilder = NotificationCompat.Builder(this, "YOUR_CALL_CHANNEL_ID")
    .setSmallIcon(R.drawable.ic_call_notification)
    .setContentTitle("来电提醒")
    .setContentText("来自联系人的通话请求")
    .setPriority(NotificationCompat.PRIORITY_HIGH)
    .setCategory(NotificationCompat.CATEGORY_CALL)
    // 关键:强制触发全屏Intent
    .setFullScreenIntent(pendingIntent, true)

NotificationManagerCompat.from(this).notify(CALL_NOTIFICATION_ID, notificationBuilder.build())

注意setFullScreenIntent的第二个参数设为true,确保锁屏时系统强制拉起全屏Activity。

2. 适配信任解锁状态的逻辑

Smart Lock/Extend Unlock会让设备处于"信任解锁"状态,系统可能跳过全屏触发逻辑。可通过KeyguardManager判断设备锁定状态,动态调整通知行为:

val keyguardManager = getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
// 判断是否处于需验证的安全锁定状态(排除信任解锁场景)
val isSecureLocked = keyguardManager.isKeyguardLocked && keyguardManager.isDeviceSecure

val notificationBuilder = NotificationCompat.Builder(this, "YOUR_CALL_CHANNEL_ID")
    // ...其他配置
    // 仅在安全锁定时触发全屏Intent,信任状态下保留通知栏跳转
    .setFullScreenIntent(if (isSecureLocked) pendingIntent else null, true)

3. 补充Activity的锁屏相关属性

在Manifest的NewCallActivity中添加更多适配属性,覆盖多版本Android系统:

<activity
    android:name=".ui.puurcall.ringingcall.NewCallActivity"
    // ...已有属性
    android:showWhenLocked="true"
    android:turnScreenOn="true"
    android:excludeFromRecents="true" />

android:showWhenLocked="true"(Android 5.0+)与代码中的setShowWhenLocked(true)形成双重保障,确保Activity在锁屏时正常显示。

4. 适配Android 12+的通知权限

Android 12及以上版本,全屏通知需要动态申请POST_NOTIFICATIONS权限,需确保权限已授予:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
        // 发起权限请求
        requestPermissions(arrayOf(Manifest.permission.POST_NOTIFICATIONS), REQUEST_NOTIFICATION_PERMISSION)
    }
}

5. 清除系统缓存后测试

重启设备或清除应用的通知缓存,避免因系统缓存导致的逻辑异常。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 03:10:56