使用 ADB 更改 Android 设备方向

问题描述 投票:0回答:5

我在真实设备上使用 Android 4.4,我想通过

adb
设置设备方向。 我不希望用 uiautomator 完成它,因为它在 uiautomator 代码终止后不会持续。

我该怎么做?

android automation adb ui-automation
5个回答
82
投票

使用“adb shell 设置”有一种更简洁的方法,而不是使用“adb shell 内容”。他们正在做同样的事情,为设置提供者赋予价值。

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°

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

33
投票

禁用

accelerometer_rotation
并设置
user_rotation


user_rotation Values:
0           # Protrait 
1           # Landscape
2           # Protrait Reversed
3           # Landscape Reversed
accelerometer_rotation Values:
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);

9
投票

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

0
投票

较新的

wm
命令有一个链接到此设置的 CLI。下面,您可以使用“wm”或“cmd window”来运行命令,但 cmd 命令是访问服务 CLI 的较新方法,例如 Activity、窗口、显示服务。

Window manager (window) commands:
  help
      Print this help text.
...
  user-rotation [-d DISPLAY_ID] [free|lock] [rotation]
    Print or set user rotation mode and user rotation.

“自由”设置不需要旋转参数。

cmd window user-rotation lock 0/1/2/3

这告诉 android 你想要如何设置旋转。一些 Android 操作系统(例如自定义 ROM、LineageOS)内置了此设置。

如果您仍然遇到应用程序不旋转的问题,或者您想要一种更用户友好的方法,您可以使用旋转控制等应用程序,或编写自己的应用程序,使用覆盖层强制 Android 旋转(在其他应用程序上绘制) )。在旋转控制中,启用控件通知后,您可以启用挂锁图标以强制旋转,即使在不受支持的应用程序中也是如此。您可以通过在 Android 屏幕覆盖层上以编程方式设置强制旋转方向来执行类似的操作。

旋转控制可能不提供通过 ADB shell 的方法,但您可以编写一个应用程序来执行此操作,或者可能与自定义通知视图交互以获得 ADB 支持。

© www.soinside.com 2019 - 2024. All rights reserved.