How to lock Android screen via ADB?

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

提交回复
热议问题