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?
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
Summry other (@Vouskopes
, etc) answer here:
XiaoMi 9
10
mDreamingLockscreen
adb shell dumpsys window | grep mDreamingLockscreen
mShowingDream=false mDreamingLockscreen=true mDreamingSleepToken=null
-> Screen Locked
ON
or OFF
mShowingDream=false mDreamingLockscreen=false mDreamingSleepToken=null
-> Scrren Unlockednfc
(if android has NFC module)adb shell dumpsys nfc | grep 'mScreenState='
mScreenState=OFF_LOCKED
-> Screen OFF and LockedmScreenState=ON_LOCKED
-> Screen ON and LockedmScreenState=ON_UNLOCKED
-> Screen ON and Unlocked