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?
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
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
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.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.
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
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