Full screen intent activity is not displayed on lookscreen when device is locked with pin/password
Asked Answered
S

0

6

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.

Stanfill answered 11/3, 2022 at 11:24 Comment(5)
I'm struggling with this problem right now with a Samsung S22 Ultra, but it will show my activity once the keyguard is dismissed. The interesting thing is if I simply press the back button when the keyguard is displayed, it will just dismiss it and show my activity. Pressing back again, then dismisses my activity and shows the key guard again as you would expect under normal conditions. Have you tried dismissing with the back button and seeing what happens?Antimere
Also when I add the listener instead of null for the keyguard, when I submit the pin it will trigger success, and the back button triggers cancel. So the listener is set and working, it's just not dismissing it on it's own for some reason?Antimere
Yeah, I have tried that with the Listener as well with the same result. What I have gathered is that the 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 the requestDismissKeyguard 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
I seem to be having good outcomes with the old deprecated way that they haven't killed yet. My screen requires a gesture so I'm not too concerned about why it says dismiss keyguard was deprecated... might hold out until it's working properly? Will need to test further. window.addFlags( WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED or WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON or WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON or WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD )Antimere
Please, any update?Capercaillie

© 2022 - 2024 — McMap. All rights reserved.