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

前端 未结 8 1301
迷失自我
迷失自我 2020-12-10 08:04

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?

相关标签:
8条回答
  • 2020-12-10 08:18

    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

    0 讨论(0)
  • 2020-12-10 08:20

    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

    0 讨论(0)
  • 2020-12-10 08:24

    Bryan's solution didn't work for my device (Samsung Galaxy S3 running version 4.4.2).

    For my KitKat GS3:

    • I can reliably tell if the screen is on by checking for mScreenOn=true (works regardless of screen lock state).
    • I can reliably tell if screen is unlocked by checking for mUserActivityTimeoutOverrideFromWindowManager=-1 (works regardless of screen ON or OFF).

    If that doesn't work for you, I'd recommend you try the following:

    1. Lock phone & turn off screen then run:

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

    1. Wake phone & keep it locked then run:

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

    1. Keep phone awake and unlock screen then run:

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

    1. Turn screen off, but don't lock it then run:

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

    1. Then use a text diffing tool (like winmerge) to look for differences between the .txt files.
    0 讨论(0)
  • 2020-12-10 08:25

    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.

    0 讨论(0)
  • 2020-12-10 08:28

    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

    0 讨论(0)
  • 2020-12-10 08:33

    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

    0 讨论(0)
提交回复
热议问题