How to lock Android screen via ADB?

后端 未结 8 920
长发绾君心
长发绾君心 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: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
    

提交回复
热议问题