How to disable keyguard lock on android fullscreen notification

Viewed 13

I'm developing an alarm like app that sends notifications with fullscreenintents

If the notification is sent when the phone is locked i want the fullscreenintent to cover the screen without needing to unlock the phone and without the keyguard lock appearing, just like in a normal alarm app.

I think I'm requesting the right permissions:

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

And I'm using both of these functions when the activity starts in hope one of them works:

private fun showWhenLockedAndTurnScreenOn(activity: Activity){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
    activity.setShowWhenLocked(true)
    activity.setTurnScreenOn(true)
    val keyguardManager = activity.getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
    keyguardManager.requestDismissKeyguard(activity, null)
} else {
    activity.window.addFlags(
        WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD or WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                or WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
    )
}

and

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)
    }
}
0 Answers
Related