Reputation: 265
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?
Upvotes: 12
Views: 25545
Reputation: 89
Omitting the lock status ; things that work on Android 12 and 13 :
IMPORTANT : those return nothing after the device been restarted.
if the three commands below return "", use "adb shell input keyevent 26" and try again.
adb shell getprop debug.tracing.screen_state
"2" means screen ON
Note : Does NOT work on older Android versions (9, 10 .. )
adb shell dumpsys input_method | grep -i screenon
Result : "screenOn = false" or "screenOn = true"
Notes :
slower than other two commands (~2 sec)
does not work on all models; use the two other options in those case
adb shell dumpsys power | grep mWakefulness= | head -1
Screen ON gives mWakefulness=Awake
Notes :
Upvotes: 2
Reputation: 4491
Worked with my Galaxy Note + (Locked with passcode)
unlock() {
if adb shell dumpsys window | grep mCurrentFocus | grep -n NotificationShade; then
adb shell 'input keyevent 26 && input keyevent 82'
if adb shell dumpsys window | grep mCurrentFocus | grep -n Bouncer; then
echo "Device was locked"
adb shell 'input text 12345678 && input keyevent 66'
fi
fi
}
Upvotes: 0
Reputation: 101
try adb shell dumpsys window | grep mCurrentFocus | grep StatusBar
;
since all unlocked status(include wrong PIN) executed by this CMD return mCurrentFocus=Window{343b978 u0 StatusBar}
example like:
#!/bin/bash
if adb shell dumpsys window | grep mCurrentFocus | grep -q StatusBar; then
echo 'phone is locked';
exit 0;
fi
echo 'phone is unlocked'
By Android 10;
Upvotes: 0
Reputation: 14318
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 UnlockedUpvotes: 6
Reputation: 821
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
Upvotes: 2
Reputation: 85
One adb command I'm using is:
adb shell dumpsys window
This returns a few system variables that are useful such as mAwake
, mShowingLockscreen
, mScreenOnEarly
, mScreenOnFully
.
To figure out which correspond to a locked/unlocked screen, I used adb shell dumpsys window > <textFileNameOfYourChoice>
tl;dr
The combination that I'm finding to be persistent is:
Device is locked AND screen is off:
mAwake=false
AND mShowingLockscreen=true
Device is locked AND screen is on:
mAwake=true
AND mShowingLockscreen=true
Device is unlocked AND screen is on:
mAwake=true
AND mShowingLockscreen=false
Upvotes: 2
Reputation: 194
This works only when device has NFC:
# returns one of: mScreenState=OFF|ON_LOCKED|ON_UNLOCKED
adb shell dumpsys nfc | grep 'mScreenState='
OFF - Screen off
ON_LOCKED - Screen displays locked screen
ON_UNLOCKED - device unlocked
Upvotes: 11
Reputation: 366
Bryan's solution didn't work for my device (Samsung Galaxy S3 running version 4.4.2).
For my KitKat GS3:
mScreenOn=true
(works regardless of screen lock state).mUserActivityTimeoutOverrideFromWindowManager=-1
(works regardless of screen ON or OFF).If that doesn't work for you, I'd recommend you try the following:
adb shell dumpsys power > dumpsys.power.screen_off.locked.txt
adb shell dumpsys power > dumpsys.power.screen_on.locked.txt
adb shell dumpsys power > dumpsys.power.screen_on.unlocked.txt
adb shell dumpsys power > dumpsys.power.screen_off.unlocked.txt
.txt
files.Upvotes: 3
Reputation: 31666
Since Lollipop PowerManager.isInteractive()
and TrustManager.isDeviceLocked()
are the proper methods to check if the device's screen is on and unlocked.
And their corresponding service call
commands would be:
adb shell service call power 12
and
adb shell service call trust 7
And this is how it can be checked from Python code without having to find Android version specific service call codes for your device - https://gist.github.com/ktnr74/60ac7bcc2cd17b43f2cb
Upvotes: 12
Reputation: 3430
If its a rooted phone you can check some fields related to lock in settings.db.
settings.db is located at /data/data/com.android.providers.settings/databases
Upvotes: 0
Reputation: 15135
This command will output everything relating to power for the device:
adb shell dumpsys power
You can pipe this to a grep to get the values of mHoldingWakeLockSuspendBlocker
and mHoldingDisplaySuspendBlocker
:
adb shell dumpsys power | grep 'mHolding'
If both are false, the display is off.
If mHoldingWakeLockSuspendBlocker
is false, and mHoldingDisplaySuspendBlocker
is true, the display is on, but locked.
If both are true, the display is on.
Upvotes: 16