关于自动化:使用ADB更改Android设备方向

Changing Android Device orientation with ADB

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

我怎样才能做到这一点?


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

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

旋转到风景:

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

旋转人像:

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


代替使用" adb shell内容",有一种更干净的方法是使用" adb shell设置"。 他们在做同样的事情,将价值提供给设置提供商。

1
2
adb shell settings put system accelerometer_rotation 0  #disable auto-rotate
adb shell settings put system user_rotation 3  #270° clockwise
  • accelerometer_rotation: auto-rotation, 0 disable, 1 enable
  • user_rotation: actual rotation, clockwise, 0 0°, 1 90°, 2 180°, 3 270°


禁用accelerometer_rotation并设置user_rotation

user_rotation Values:

1
2
3
4
0           # Protrait
1           # Landscape
2           # Protrait Reversed
3           # Landscape Reversed

accelerometer_rotation Values:

1
2
0           # Stay in the current rotation
1           # Rotate the content of the screen

使用adb的示例:

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

以编程方式示例:

1
2
3
4
5
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);