Home » Android » android – How to tell if screen is on with ADB

android – How to tell if screen is on with ADB

Posted by: admin June 15, 2020 Leave a comment

Questions:

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?

How to&Answers:

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

Answer:

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.