Thursday, 9 May 2019

Disabling lock screen must not trigger Standby screen after 30 seconds on Android 7.0 and lower

I've written a method that disables the Android lock screen and makes the screen turn on from standby (for dialing). This method works correctly for 30 seconds but then it triggers the standby screen that flashes briefly. This triggers onPause, onStop, onResume. Which shouldn't happen. Which Flag is not correct? Implementation must work from latest Android version till API 19.

This bugs only occurs on Android devices < 8.0.

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    disableLockScreen(this)
    setContentView(R.layout.activity_main)
    Log.i("AppLifecycleTest", "onCreate")
}

override fun onResume() {
    super.onResume()
    Log.i("AppLifecycleTest", "onResume")
}

override fun onPause() {
    super.onPause()
    Log.i("AppLifecycleTest", "onPause")
}

override fun onStop() {
    super.onStop()
    Log.i("AppLifecycleTest", "onStop")
}

override fun onDestroy() {
    super.onDestroy()
    Log.i("AppLifecycleTest", "onDestroy")
}

@Suppress("DEPRECATION")
private fun disableLockScreen(activity: Activity) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
        activity.setShowWhenLocked(true)
        activity.setTurnScreenOn(true)
    }
    val lock =
        (activity.getSystemService(Activity.KEYGUARD_SERVICE) as KeyguardManager).newKeyguardLock(Context.KEYGUARD_SERVICE)
    val powerManager = activity.getSystemService(Context.POWER_SERVICE) as PowerManager
    val wake = powerManager.newWakeLock(
        PowerManager.FULL_WAKE_LOCK or
                PowerManager.ACQUIRE_CAUSES_WAKEUP or
                PowerManager.ON_AFTER_RELEASE,
        "Beterdichtbij:BusSnoozeAlarm"
    )

    lock.disableKeyguard()
    // This timeout doesn't seem to do anything
    wake.acquire(6 * 1000L)

    activity.window.addFlags(
        WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                or WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                or WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                or WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
                or WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON
    )
}

Start condition: remove App from memory and start so that screen is active.

LGE Nexus 5X: (Android 7.0, API 24) After exactly 30 seconds Screen flashes quickly and shows standby menu, and the toggle button dissapears. This triggers onPause, onStop, onResume. This shouldn't happen.

2019-05-07 10:14:40.613 AppLifecycleTest: onCreate
2019-05-07 10:14:40.616 AppLifecycleTest: onResume
2019-05-07 10:15:10.851 AppLifecycleTest: onPause
2019-05-07 10:15:10.891 AppLifecycleTest: onStop
2019-05-07 10:15:10.994 AppLifecycleTest: onResume

Samsung Galaxy S8: (Android 8.0, API 26) After 30 seconds of not interacting with the screen, the toggle button disapears but no App lifecycles are triggered and no quick flashing of the standby screen. This is correct.

2019-05-07 10:23:22.654 AppLifecycleTest: onCreate
2019-05-07 10:23:22.717 AppLifecycleTest: onResume

This SO thread has the same problem, onStop triggers after 30 secs.

Here is my work-around for this, which works but isn't conform the documentation:

// add to onCreate
private fun initTimer() {
    timer = Timer()
    timer.schedule(25000L) {
        activity.runOnUiThread {
            disableLockScreen(activity)
            initTimer()
            Log.i("AppLifecycleTest", "reset every 25 Seconds")
        }
    }
}



from Disabling lock screen must not trigger Standby screen after 30 seconds on Android 7.0 and lower

No comments:

Post a Comment