Search code examples
androidtestingadbmonkeyrunner

How to tell if screen is on with ADB


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?


Solution

  • 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