使用ADB更改Android设备方向

42

我正在使用真实设备上的 Android 4.4,并希望通过 adb 设置设备方向。 我不想使用 uiautomator 来完成,因为在 uiautomator 代码终止后它不会持续存在。

我该如何做?


1
可能是Android模拟器-屏幕旋转的重复问题。 - Simon Featherstone
4个回答

79

使用 "adb shell settings" 而不是 "adb shell content" 更加简洁。它们是在向设置提供程序中放置值方面做着相同的事情。

adb shell settings put system accelerometer_rotation 0  #disable auto-rotate
adb shell settings put system user_rotation 3  #270° clockwise
  • accelerometer_rotation: 自动旋转,0 禁用,1 启用
  • user_rotation: 实际旋转,顺时针,0 0°,1 90°,2 180°,3 270°

在Firestick上,我的屏幕自动旋转,我不得不将user_rotation设为0,这样就停止了,不确定它们是否有不同意义的这些值(使用的命令:settings put system user_rotation 0)。 - Mazzy
这个帮助我恢复了我的Waydroid实例,之前我最大化一个应用程序后,一切都旋转了。 - hacker1024

72

你可能需要先关闭自动旋转:

adb shell content insert --uri content://settings/system --bind name:s:accelerometer_rotation --bind value:i:0

旋转至横屏:

adb shell content insert --uri content://settings/system --bind name:s:user_rotation --bind value:i:1

旋转竖屏:

adb shell content insert --uri content://settings/system --bind name:s:user_rotation --bind value:i:0

3
您也可以使用数字 23 分别表示倒立的纵向和反转的横向。 - Matthew Read
9
您需要运行以下命令以关闭自动旋转:adb shell content insert --uri content://settings/system --bind name:s:accelerometer_rotation --bind value:i:0。请注意,翻译时不得改变原文意义,并尽量简洁易懂。 - Patrick Boos
1
我该如何避免关闭自动旋转?以及如何获取和恢复其原始值? - android developer
运作得非常好,禁用自动旋转非常重要。 - vishalm
content insert --uri content://settings/system is exactly the same thing as writing settings put system - Zibri

32

禁用 accelerometer_rotation 并设置 user_rotation


user_rotation 值:
0           # Protrait 
1           # Landscape
2           # Protrait Reversed
3           # Landscape Reversed
加速度计旋转值:
0           # Stay in the current rotation
1           # Rotate the content of the screen

使用adb的示例:


adb shell settings put system accelerometer_rotation 0
adb shell settings put system user_rotation 3

编程示例:

import android.provider.Settings;

// You can get ContentResolver from the Context
Settings.System.putInt(getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0);
Settings.System.putInt(getContentResolver(), Settings.System.USER_ROTATION, 3);

6

wm cmd 可用于在 adb shell 上设置用户旋转

wm help
user-rotation [free|lock] [-d DISPLAY_ID] [rotation]
Set user rotation mode and user rotation.

例子:

wm user-rotation lock 0
wm user-rotation lock 1

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