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

I know that PowerManager and / or KeyguardManager can help me check if the device’s screen is locked or unlocked. Is there any way to check this with adb?

+4
android adb
source share
7 answers

This command will display everything related to the power for the device:

adb shell dumpsys power 

You can pass this to grep to get the values ​​of mHoldingWakeLockSuspendBlocker and mHoldingDisplaySuspendBlocker :

 adb shell dumpsys power | grep 'mHolding' 

If both values ​​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.

+6
source share

Since Lollipop PowerManager.isInteractive() and TrustManager.isDeviceLocked() are the correct methods to check if the device screen is turned on and unlocked.

And their respective service call commands:

 adb shell service call power 12 

and

 adb shell service call trust 7 

And here's how you can verify it from Python code without requiring call codes for a specific version of Android for your device - https://gist.github.com/ktnr74/60ac7bcc2cd17b43f2cb

+5
source share

This only works when the device has NFC:

 # returns one of: mScreenState=OFF|ON_LOCKED|ON_UNLOCKED adb shell dumpsys nfc | grep 'mScreenState=' 

OFF - OFF

ON_LOCKED - The screen displays a locked screen.

ON_UNLOCKED - device is unlocked

+5
source share

Bryan's solution does not work for my device (Samsung Galaxy S3 works version 4.4.2).

For my KitKat GS3:

  • I can say with confidence whether the screen is turned on by checking mScreenOn=true (it works regardless of the screen lock status).
  • I can confidently say if the screen is unlocked by checking mUserActivityTimeoutOverrideFromWindowManager=-1 (it works regardless of whether the screen is on or off).

If this does not work for you, I recommend that you try the following:

  • Lock the phone and turn off the screen, then run:

adb shell dumpsys power > dumpsys.power.screen_off.locked.txt

  1. Ring the phone and lock it, then run:

adb shell dumpsys power > dumpsys.power.screen_on.locked.txt

  1. Launch the wake up phone and unlock the screen:

adb shell dumpsys power > dumpsys.power.screen_on.unlocked.txt

  1. Turn off the screen, but do not block it, and then run:

adb shell dumpsys power > dumpsys.power.screen_off.unlocked.txt

  1. Then use a text tool (like winmerge) to look for differences between .txt files.
+1
source share

I am using adb command:

adb shell dumpsys window

This returns several useful system variables, such as mAwake , mShowingLockscreen , mScreenOnEarly , mScreenOnFully .

To find out what corresponds to a locked / unlocked screen, I used adb shell dumpsys window > <textFileNameOfYourChoice>

TL; dr

A combination that I consider constant:

The device is locked AND the screen is off: mAwake=false AND mShowingLockscreen=true

The device is locked AND screen mAwake=true : mAwake=true AND mShowingLockscreen=true

The device is unlocked AND the screen mAwake=true : mAwake=true AND mShowingLockscreen=false

+1
source share

If on your root phone you can check some of the fields related to the lock in settings.db.

settings.db is located in /data/data/com.android.providers.settings/databases

0
source share

Connect your phone and run this code.

Press the power button and see what changes are occurring.

Unlock your phone and see what changes have occurred.

Experiment. Have some 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 example, these are the changes that occur after locking the phone and the black screen:

 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
source share

All Articles