Googling and checking SO answers gives me many working options on how to turn the screen on for Android, like this one. But all of them uses one or more deprecated methods or enums. If I try the replacements, they don't work. My goal is to do this at the start of instrumentation tests without using deprecated methods.
Here are my attempts
A)
val power = context.getSystemService(POWER_SERVICE) as PowerManager
val wakeLock = power.newWakeLock(FULL_WAKE_LOCK or ACQUIRE_CAUSES_WAKEUP or ON_AFTER_RELEASE, "tag")
wakeLock.acquire()
Works, but uses deprecated enum FULL_WAKE_LOCK
. Removing that flag causes an error telling me to specify a level. The deprecation doc says I should rather use android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
instead. So I try that in B:
B)
In onCreate
:
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
This did not turn the screen on.
C)
I also noticed there are a couple of interesting flags for the activity to set in the manifest. I tried:
android:turnScreenOn="true"
android:showWhenLocked="true"
This did not turn the screen on
While solution A works, I strive to use non-deprecated functions. This is more of an academic question. Does anyone know how to programatically turn the screen on while not using deprecated functions or constants?