Is there a way to check if Android device screen i

2019-01-24 23:10发布

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?

标签: android adb
6条回答
萌系小妹纸
2楼-- · 2019-01-24 23:13

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

查看更多
叼着烟拽天下
3楼-- · 2019-01-24 23:14

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

查看更多
淡お忘
4楼-- · 2019-01-24 23:16

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

查看更多
等我变得足够好
5楼-- · 2019-01-24 23:19

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.
查看更多
成全新的幸福
6楼-- · 2019-01-24 23:30

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

查看更多
别忘想泡老子
7楼-- · 2019-01-24 23:37

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.

查看更多
登录 后发表回答