How to lock Android screen via ADB?

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

    Michael R. Hines gave the what is arguably the easiest solution. However, the following line is not useful in later versions of Android.

    adb shell input keyevent 82 # unlock
    

    I've updated the shell script using coordinates for the individual device I want to wake (Tablet). My tablet does not support orientation changes for lockscreen events, so the values always work as the lockscreen is always in landscape. Should you require orientation change detection, a simple if/then/else would suffice in picking the correct coordinates to use for the orientation.

    #!/bin/bash
    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 touchscreen swipe 930 380 1080 380 # unlock
        echo "OK, should be on now."
    else 
        echo "Screen is already on."
        echo "Turning off."
        adb shell input keyevent 26 # sleep
    fi
    
    0 讨论(0)
  • 2021-01-30 03:58

    For those using earlier versions of android (4.2+ at least), dumpsys power has a different output.
    Instead of using mPowerState= as the answer given by @Jakub Czaplicki, I used mScreenOn=.

    p = Runtime.getRuntime().exec("su", null, null);
    OutputStream o = p.getOutputStream();
    o.write(("dumpsys power").getBytes("ASCII"));
    o.flush();
    o.close();
    p.waitFor();
    
    boolean screenState;
    BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
    while ((res = in.readLine()) != null) dump += res;
    screenState = dump.charAt( dump.indexOf("mScreenOn=") + 10 ) == 't';
    

    screenState is true (screen on), or false (screen off).

    0 讨论(0)
提交回复
热议问题