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

前端 未结 8 1302
迷失自我
迷失自我 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
    
    0 讨论(0)
  • 2020-12-10 08:42

    Summry other (@Vouskopes, etc) answer here:

    • My Phone: XiaoMi 9
      • Android: 10

    use adb to check status of screen locked

    method 1: (universal) use mDreamingLockscreen

    • Command: adb shell dumpsys window | grep mDreamingLockscreen
    • Output:
      • mShowingDream=false mDreamingLockscreen=true mDreamingSleepToken=null -> Screen Locked
        • no matter Screen is ON or OFF
      • mShowingDream=false mDreamingLockscreen=false mDreamingSleepToken=null -> Scrren Unlocked

    method 2: use nfc (if android has NFC module)

    • Command: adb shell dumpsys nfc | grep 'mScreenState='
    • Output:
      • mScreenState=OFF_LOCKED -> Screen OFF and Locked
      • mScreenState=ON_LOCKED -> Screen ON and Locked
      • mScreenState=ON_UNLOCKED -> Screen ON and Unlocked
    0 讨论(0)
提交回复
热议问题