通过ADB检查设备是否为横屏模式

7

通过ADB检查设备方向是否可能?

不需要安装任何软件,调用任何现有软件,只需通过ADB即可。猜测在/proc某处存在状态文件,但尚未找到。

6个回答

14

这可以通过以下命令完成:

adb shell dumpsys | grep 'SurfaceOrientation' | awk '{ print $2 }'

每个方向的输出将是一个整数,范围从0到3。0和2为横向,1和3为竖向。由于dumpsys输出非常大,该命令可能需要几秒钟才能完成。

更新:dgmltn的修改可能会更快:

adb shell dumpsys input | grep 'SurfaceOrientation' | awk '{ print $2 }'

太神奇了!执行该命令需要一段时间。%time adb shell => 在Nexus 7上大约需要9秒钟,输出为adb shell dumpsys 0.01s user 0.03s system 0% cpu 9.001 total。无论如何,你做得很好。谢谢! - Sebastian Roth
1
@TVK:0 表示竖屏,1 表示横屏,你说反了。不管怎样,谢谢。 - Oxi
也许在每个设备上都不同。我非常确定这些值在早期的Galaxy Tab型号上是正确的。 - tvkanters
8
如果您将dumpsys限制为输入,速度会更快:adb shell dumpsys input | grep 'SurfaceOrientation' | awk '{ print $2 }' - dgmltn
这在Android 13上不起作用,“SurfaceOrientation”在dumpsys的输出中不存在。 - virmis_007
显示剩余2条评论

1

在Pixel和Pixel Tablet上验证过,可以使用

adb shell dumpsys window displays | grep 'DisplayFrmaes'

结果类似于DisplayFrames w=1080 h=2340 r=0

r=0,1,2,3是显示方向。


1
我不确定自从上次回答写下来以来是否有什么改变,或者是因为我正在模拟不同的设备。 但是对于我来说,无论是SurfaceOrientation还是mLandscapeRotation都没有在dumpsys中显示出来。 在旋转之间比较dumpsys的输出后,我发现了mPredictedRotation
#Normal
$./adb shell dumpsys window displays | grep 'mPredictedRotation'
        mPredictedRotation=0

#Right
$./adb shell dumpsys window displays | grep 'mPredictedRotation'
        mPredictedRotation=3

#Inverted
$./adb shell dumpsys window displays | grep 'mPredictedRotation'
        mPredictedRotation=2
$Left
#./adb shell dumpsys window displays | grep 'mPredictedRotation'
        mPredictedRotation=1

0

更简单的解决方案:

adb shell dumpsys window | grep -i surfaceorientation | awk '{ print $2 }'

0

在打开adb shell后,我发现了这个方法:content query --uri content://settings/system --projection name:value --where "name='user_rotation'"。虽然如果不先打开shell输入似乎无法正常工作。


0
除了之前的回答,我想指出 adb shell dumpsys | grep 'SurfaceOrientation' 的返回值在不同设备上并不总是遵循特定规则("0 和 2 是横屏,1 和 3 是竖屏" 是错误的)。
我通过额外的查询改进了这种方法,它可以建议如何解释返回值,即
dumpsys window | grep 'mLandscapeRotation'

数值0表示:0和2为横向,1和3为纵向

数值1表示相反的情况


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接