当设备使用 PIN/密码锁定时,全屏意图活动不会显示在外观屏幕上
我想在用户锁定设备后立即在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论