How to tell if screen is on with ADB [duplicate]
Asked Answered
T

2

8

I am looking to find out whether or not it is possible to determine if the screen is on on an android device using ADB. I need to know this for some tests I am trying to run using monkey runner. Is there a shell command I can enter, and thus include as part of a monkey runner command, that will tell me definitively if the screen is on or off?

Tartarus answered 28/1, 2014 at 14:58 Comment(2)
https://mcmap.net/q/1041617/-obtain-screen-status-using-adbDolce
@AlexP. Yeah it is, I missed that while looking for this answer.Tartarus
C
11

In doing some testing I've found that using adb shell dumpsys power | grep mScreenOn will work on devices that have a version number of 4.2+

The command that I have found to work on all devices I have tested so far is to use:

 adb shell dumpsys input_method | grep mScreenOn

which will produce something like:

mSystemReady=true mScreenOn=true

which you can use to determine if the screen is on.

Tested on all Android Emulators in the range 2.2 - 4.4.2, Samsung Galaxy SII (4.0.4), Samsung Galaxy Tab 8.9 (4.0.4), and Nexus 4 with CM11

Also worth mentioning, on pre 4.2 devices you can use the command adb shell dumpsys power | grep mPowerState to get something like this:

mIsPowered=true mPowerState=3 mScreenOffTime=24970 ms
mPowerState=SCREEN_BRIGHT_BIT SCREEN_ON_BIT

and detect if the SCREEN_ON_BIT string is present

Chiquitachirico answered 28/1, 2014 at 23:47 Comment(2)
On Lollipop (Nexus 6), the first command responds with mInteractive=true when the screen is on, or mInteractive=false when off; and the second command responds with Display Power: state=ON when on, and state=OFF when the screen is off.Squawk
On my Sony Bravia Android 8 TV, dumpsys power has no mScreenOn. But I see mWakefulness=Asleep when the TV screen is off, mWakefulness=Awake when the TV screen is on.Counterblast
T
4

Yes, if you enter:

 adb shell dumpsys power | grep mScreenOn

This will return a true or false value telling you whether or not the screen is currently on. It should look like this:

mScreenOn=true

Knowing this, all you need to do is parse the true/false value out of the result, and feed the shell command into a monkey runner script.

This was tested on an android device running 4.4.2.

Tartarus answered 28/1, 2014 at 14:58 Comment(3)
The service to dump may vary depending on the device you are using. On my Nexus 4 emulator I needed to dump input_method instead of power.Chiquitachirico
@CarloB. That is also good to know, I have a physical Nexus 4 and 5 both running 4.4.2, both will respond to dumpsys power. I'm curious if your command is specific to the emulator, or a specific version of android.Tartarus
Answer as of today: grep for mWakefulness in the same command's output. Tested on Sony Bravia 2023 model.Arabinose

© 2022 - 2024 — McMap. All rights reserved.