如何使用 adb shell 命令验证 android 设备屏幕打开或关闭

How to verify android device Screen ON or OFF using adb shell command

尝试使用 mScreenOn=truemPowerState=SCREEN_BRIGHT_BIT 检查设备屏幕打开或关闭。但是以下命令在最新的 android 版本中不起作用。它什么都不返回

以下命令在 Android - 4.3 Jelly Bean

中运行良好
  1. 使用 input_method dumpsys

    adb shell dumpsys input_method | grep mScreenOn

  2. 使用 power dumpsys

    adb shell dumpsys power | grep mScreenOn

    adb shell dumpsys power | grep mPowerState

在最新的 android 版本(Lollipop、Nougat、Oreo、Pie 等)上使用 adb shell 命令是否有任何其他方法来验证屏幕关闭或打开状态

Android 5.0.1 – Lollipop 行为改变了,他们删除了日志记录 mScreenOn

试了很多次,比较dumpsys input_method 文件后发现

adb shell dumpsys input_method | grep -i mActive

最近我遇到了同样的问题,找到了以下解决方案。

mInteractive 值在 dumpsys 中为 "true" input_method 显示开启,"false" 显示关闭。

Ex 在 shell 脚本中的用法:

screen_info=`adb shell dumpsys input_method | grep mInteractive=true`
if [[ $screen_info == *"mInteractive"* ]]
then
    echo "Screen is ON"
     #Do something
else
    echo "Screen is OFF"
    #Do something
fi

android - Obtain screen status using ADB - Stack Overflow 的答案,复制到这里:

  • 我的Phone:XiaoMi 9
    • Android: 10

使用 adb 检查屏幕状态 ON/OFF ?

方法一:使用mHoldingDisplaySuspendBlocker

  • 命令:adb shell dumpsys power | grep mHoldingDisplaySuspendBlocker
  • 输出:
    • mHoldingDisplaySuspendBlocker=false -> 屏幕 关闭
    • mHoldingDisplaySuspendBlocker=true -> 屏幕 ON

方法二:使用mWakefulness

  • 命令:adb shell dumpsys power | grep mWakefulness=
  • 输出:
    • mWakefulness=Dozing -> 屏幕 关闭
    • mWakefulness=Awake -> 屏幕 ON

方法三:使用nfc(如果android有NFC模块)

  • 命令:adb shell dumpsys nfc | grep mScreenState=
  • 输出:
    • mScreenState=OFF_LOCKED -> 屏幕 关闭(并锁定)
    • mScreenState=ON_XXX -> 屏幕 开启
      • mScreenState=ON_LOCKED -> 屏幕开启(并且锁定
      • mScreenState=ON_UNLOCKED -> 屏幕开启(并且解锁

方法四:使用service call

  • 命令:adb shell service call power 12
  • 输出:
    • Result: Parcel(00000000 00000000 '........') -> 0 表示屏幕 OFF
    • Result: Parcel(00000000 00000001 '........') -> 1 表示屏幕 ON