How to lock Android screen via ADB?

后端 未结 8 917
长发绾君心
长发绾君心 2021-01-30 03:20

Is there a way to lock the Android screen via the ADB?

I find ways to lock the display in an apk, but I want to lock the screen from the PC via ADB, to simulate a displ

相关标签:
8条回答
  • 2021-01-30 03:45

    You can use following command to trigger display ON. adb shell input keyevent POWER

    I tried and am using in my project, Hope it will work for you.

    And here is the ruby code I used:

    def ScreenCheck()
    system("adb shell dumpsys power > c:/interact.log")
    
    File.open("C:\\interact.log").each do |line|
        if line[/mScreenOn/]
            if line.strip == "mScreenOn=true"
                p "Screen is On, Starting execution.."
            else
                p "Screen is Off, starting screen.."
                system("adb shell input keyevent = POWER")
                p "Starting execution.."
            end
        end
    end
    end
    
    0 讨论(0)
  • 2021-01-30 03:46

    Cool, I just found KEYCODE_POWER which is 26.

    so it works by sending:

    adb shell input keyevent 26
    

    which locks the screen if the screen is unlocked. If the screen is already locked, it wakes up the device.

    My guess is that the only way to ensure that the screen is locked (off), is to unlock (we use keyevent 82 (menu), then lock it with the power button keyevent. Does anyone have any idea if this is true?

    0 讨论(0)
  • 2021-01-30 03:46

    You've already found a solution, but I'll put this code here for reference anyway.

    What you could do is to inject event to "press" the power button twice. If you don't know the status of the device (display on/off), check whether the screen is currently on or off and press the power button accordingly.

    Here's a simple monkeyrunner script:

    import re
    from java.util import *
    from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
    device = MonkeyRunner.waitForConnection()       # connect to a device
    device.shell("input keyevent KEYCODE_POWER")    # turn screen off (or on?)
    res = device.shell("dumpsys power")             # fetch power state
    m = re.search(r'.*mPowerState=([0-9]+).*', res) # parse the string
    if m and int(m.group(1)) == 0:                  # screen is off
      device.shell("input keyevent KEYCODE_POWER")  # turn the screen on
    
    0 讨论(0)
  • 2021-01-30 03:49

    In addition to the answers before, here's what I do to lock / unlock my screen using adb:

    adb shell input keyevent 26 will lock the screen.
    So, if you execute that command again, while the screen is turned off / locked, it will be turned on / unlocked.
    adb shell input keyevent 26 will also unlock the screen (if the screen is locked).

    Furthermore, I have also tested all commands, such as adb shell input keyevent number, and found out that adb shell input keyevent 3 also unlock the device.

    I had also found out (by testing) that key 3 is the home button. So , if you have a physical home button (not the soft home button on the screen), you can also use this to unlock your device.

    0 讨论(0)
  • 2021-01-30 03:50

    Here's the whole thing in one single bash script which checks if the screen is actually on or not and then wakes up and unlocks the screen in one shot:

    if [ "$(adb shell dumpsys power | grep mScreenOn= | grep -oE '(true|false)')" == false ] ; then
        echo "Screen is off. Turning on."
        adb shell input keyevent 26 # wakeup
        adb shell input keyevent 82 # unlock
        echo "OK, should be on now."
    else 
        echo "Screen is already on."
    fi
    
    0 讨论(0)
  • 2021-01-30 03:55

    Here is a script to turn on/off the screen for every connected device including any pre-lollipop devices. I use this on my Jenkins server right before running any connected Android tests to make sure the devices are ready to go. Hope someone finds this useful!

    #!/bin/sh
    
    # Returns the power state of the screen 1 = on, 0 = off
    getDisplayState() {
        state=$(adb -s $1 shell dumpsys power | grep mScreenOn= | grep -oE '(true|false)')
    
        # If we didn't get anything it might be a pre-lollipop device
        if [ "$state" = "" ]; then
            state=$(adb -s $1 shell dumpsys power | grep 'Display Power' | grep -oE '(ON|OFF)')
        fi
    
        if [ "$state" = "ON" ] || [ "$state" = "true" ]; then
            return 1;
        else
            return 0;
        fi
    }
    
    echo "Turning on screen on all connected devices..."
    
    for device in `adb devices | grep device$ | cut -f1`
    do
        echo -n "Found device: $device ... "
    
        getDisplayState $device
        state=$?
    
        # If the display is off, turn it on
        if [ $state -eq 0 ]; then
            echo "display was off, turning on"
            adb -s $device shell input keyevent 26
        else
            echo "display was on"
        fi
    
    done
    
    0 讨论(0)
提交回复
热议问题