1

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?

Nilzor
  • 18,082
  • 22
  • 100
  • 167
  • 1
    Does this answer your question? [Android Espresso, Wake up device before test. How to use a custom manifest for test?](https://stackoverflow.com/questions/19870771/android-espresso-wake-up-device-before-test-how-to-use-a-custom-manifest-for-t) – Buddy Christ Mar 27 '20 at 08:58
  • No, the answer uses the depreacted constant `WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON` – Nilzor Mar 30 '20 at 07:44

0 Answers0