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.
requestDismissKeyguard
actually opens the keyguard for the user so that they then can dismiss it to unlock the phone. So I went with the version without therequestDismissKeyguard
for now as this works fine with 3 out of 4 testing devices. Still couldn't figure out why it doesn't dimiss it or show it at all for other devices, my guess it might be some special permissions or battery saving options? – Stanfill