当设备使用 PIN/密码锁定时,全屏意图活动不会显示在外观屏幕上

发布于 2025-01-13 07:44:38 字数 2118 浏览 0 评论 0原文

我想在用户锁定设备后立即在 Android 外观屏幕上显示一份简短的调查问卷。为此,我检测屏幕锁定事件并显示带有全屏意图通知的活动。

 val fullScreenIntent = Intent(context, destination)
        fullScreenIntent.flags = Intent.FLAG_ACTIVITY_NO_HISTORY or
            Intent.FLAG_ACTIVITY_CLEAR_TASK or
            Intent.FLAG_ACTIVITY_CLEAR_TOP or
            Intent.FLAG_ACTIVITY_NEW_TASK

        val fullScreenPendingIntent = PendingIntent.getActivity(context, 0, fullScreenIntent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)

        val builder = NotificationCompat.Builder(context, CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_logo)
            .setContentTitle(title)
            .setContentText(description)
            .setFullScreenIntent(fullScreenPendingIntent, true)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setCategory(NotificationCompat.CATEGORY_ALARM)

        with(notificationManager){
            createNotificationChannel()
            val notification = builder.build()
            notify(NOTIFICATION_ID, notification)
        }

为了允许活动显示在外观屏幕上,我在调查问卷活动的 OnCreate 方法中执行此操作:

fun Activity.turnScreenOnAndKeyguardOff() {
    setShowWhenLocked(true)
    setTurnScreenOn(true)

    with(getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager) {
        requestDismissKeyguard(this@turnScreenOnAndKeyguardOff, null)
    }
}

在清单中:

 <activity
        android:name="com.example.trackingapp.activity.LockActivity"
        android:exported="true"
        android:launchMode="singleTop"
        android:showOnLockScreen="true"
        android:excludeFromRecents="true"/>

这适用于 Android 9、10 和 11 以及某些 Android 12 台(Pixel 3)设备。但在某些 Android 12 设备上(我已在 Samsung A42 和 Pixel 4 上进行了测试),当设备配置了 PIN 或密码时,仅显示 PIN 键盘保护覆盖层。如果用户输入 PIN 码,设备就会解锁并且不会显示任何活动。

我也试过了,

 fun Activity.turnScreenOnAndKeyguardOff() {
    setShowWhenLocked(true)
    setTurnScreenOn(true)
 }
 

但是只有屏幕亮了。据我通过调试所看到的,活动的通知已创建,但立即自行完成。

有谁知道如何使用 PIN 码可靠地在锁定屏幕上显示活动,或者问题可能是什么?

提前致谢。

I want to show a short questionnaire on the Android look screen, as soon as the user locks their device. For this I detect a screen lock event and show an activity with a full screen intent notification.

 val fullScreenIntent = Intent(context, destination)
        fullScreenIntent.flags = Intent.FLAG_ACTIVITY_NO_HISTORY or
            Intent.FLAG_ACTIVITY_CLEAR_TASK or
            Intent.FLAG_ACTIVITY_CLEAR_TOP or
            Intent.FLAG_ACTIVITY_NEW_TASK

        val fullScreenPendingIntent = PendingIntent.getActivity(context, 0, fullScreenIntent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)

        val builder = NotificationCompat.Builder(context, CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_logo)
            .setContentTitle(title)
            .setContentText(description)
            .setFullScreenIntent(fullScreenPendingIntent, true)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setCategory(NotificationCompat.CATEGORY_ALARM)

        with(notificationManager){
            createNotificationChannel()
            val notification = builder.build()
            notify(NOTIFICATION_ID, notification)
        }

To allow the activity to show up on the lookscreen, I do this in the OnCreate method of the questionnaire activity:

fun Activity.turnScreenOnAndKeyguardOff() {
    setShowWhenLocked(true)
    setTurnScreenOn(true)

    with(getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager) {
        requestDismissKeyguard(this@turnScreenOnAndKeyguardOff, null)
    }
}

And in the manifest:

 <activity
        android:name="com.example.trackingapp.activity.LockActivity"
        android:exported="true"
        android:launchMode="singleTop"
        android:showOnLockScreen="true"
        android:excludeFromRecents="true"/>

This works as intended for Android 9, 10 and 11 and on some Android 12 (Pixel 3) devices. But on some Android 12 devices (I have tested on Samsung A42 and Pixel 4) when the device is configured with a PIN or Password, only the PIN keyguard overlay gets shown. And if the user puts in their pin the device gets unlocked and no activity is displayed.

I have also tried just

 fun Activity.turnScreenOnAndKeyguardOff() {
    setShowWhenLocked(true)
    setTurnScreenOn(true)
 }
 

but then only the screen turns on. The notification with the activity gets created but instantly finishes itself, as far as I can see with debugging.

Has anyone an idea how to reliable show the activity on the lock-screen with a PIN in place or what the problem might be?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文