Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

adb

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?

like image 222
saras Avatar asked Feb 08 '16 17:02

saras


3 Answers

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.

like image 191
Bryan Avatar answered Oct 08 '22 08:10

Bryan


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

like image 22
Alex P. Avatar answered Oct 08 '22 10:10

Alex P.


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

like image 35
Vouskopes Avatar answered Oct 08 '22 10:10

Vouskopes