Is there a way to check if Android device screen is locked via adb?

前端 未结 8 1313
迷失自我
迷失自我 2020-12-10 08:04

I know that PowerManager and/or KeyguardManager can help me check if a device screen is locked/unlocked. Is there a way to check this via adb?

8条回答
  •  孤城傲影
    2020-12-10 08:35

    Attach a phone and run this code.

    Press the power button, and see the changes that happen.

    Unlock the phone and see the changes that happen.

    Experiment. Have fun.

    import re
    import subprocess
    import time
    
    states = {
        'no_cached_wake_locks': '',
        'mDirty': '',
        'mWakefulness': '',
        'mWakefulnessChanging': '',
        'mIsPowered': '',
        'mPlugType': '',
        'mBatteryLevel': '',
        'mBatteryLevelCriticalLow': '',
        'mLastBatteryLevelCriticalLowTime': '',
        'mBatteryLevelWhenDreamStarted': '',
        'mDockState': '',
        'mStayOn': '',
        'mProximityPositive': '',
        'mBootCompleted': '',
        'mSystemReady': '',
        'mHalAutoSuspendModeEnabled': '',
        'mHalInteractiveModeEnabled': '',
        'mWakeLockSummary': '',
        'mUserActivitySummary': '',
        'mRequestWaitForNegativeProximity': '',
        'mSandmanScheduled': '',
        'mSandmanSummoned': '',
        'mLowPowerModeEnabled': '',
        'mBatteryLevelLow': '',
        'mLightDeviceIdleMode': '',
        'mDeviceIdleMode': '',
        'mScreenBrightnessBoostInProgress': '',
        'mDisplayReady': '',
        'mHoldingWakeLockSuspendBlocker': '',
        'mHoldingDisplaySuspendBlocker': '',
    }
    
    
    def checkit():
        cmd = ['adb', 'shell', 'dumpsys', 'power']
        proc = subprocess.Popen(cmd,
                            bufsize=0,
                            universal_newlines=True,
                            stdin=None,
                            stdout=subprocess.PIPE,
                            stderr=subprocess.STDOUT)
    
        changes = 0
        for line2 in proc.stdout.readlines():
        line = line2.strip()
        for state, value in states.items():
            m = re.search(r'{}=(.*)'.format(state), line)
            if m:
                if value != m.group(1):
                    changes += 1
                    print("changed: state={} old={} new={}".format(state, value, m.group(1)))
                    states[state] = m.group(1)
        if changes > 0:
        print("---- {} changes".format(changes))
    
    
    while True:
        checkit()
        time.sleep(0.5)
    

    For exanple, these are the changes that happen after you lock the phone and the screen is black:

    changed: state=mWakefulness old=Awake new=Asleep
    changed: state=mHalAutoSuspendModeEnabled old=false new=true
    changed: state=mHalInteractiveModeEnabled old=true new=false
    changed: state=mUserActivitySummary old=0x4 new=0x0
    changed: state=mHoldingDisplaySuspendBlocker old=true new=false
    ---- 5 changes
    changed: state=mWakeLockSummary old=0x1 new=0x0
    changed: state=mHoldingWakeLockSuspendBlocker old=true new=false
    ---- 2 changes
    changed: state=mWakeLockSummary old=0x0 new=0x1
    changed: state=mHoldingWakeLockSuspendBlocker old=false new=true
    ---- 2 changes
    changed: state=mWakeLockSummary old=0x1 new=0x0
    changed: state=mHoldingWakeLockSuspendBlocker old=true new=false
    ---- 2 changes
    

提交回复
热议问题