我有一个 Android PC root(开箱即用)连接到始终横向显示的外部显示器(HDMI 和 USB),即使我的应用程序在 list 中的 Activity 声明中指定了纵向:
android:screenOrientation="portrait"
我正在尝试从此 SO post 执行以下命令强制肖像:
proc = Runtime.getRuntime().exec("adb shell content insert --uri content://settings/system --bind name:s:user_rotation --bind value:i:0");
int exitVal = proc.waitFor();
这应该将屏幕设置为纵向,但显示始终保持横向。没有记录异常。
我也使用了以下但没有成功:
process = Runtime.getRuntime().exec("content insert --uri content://settings/system --bind name:s:user_rotation --bind value:i:0");
process = Runtime.getRuntime().exec(new String[]{"su","-c","content insert --uri content://settings/system --bind name:s:user_rotation --bind value:i:0"});
在这两种情况下,退出值都是[0]。这意味着命令正常终止。
Android PC 上只有一个 USB 端口和一个微型 USB 端口(仅电源/无数据)。启用调试后,我无法在 DDMS 中枚举设备,也无法从将我的开发 PC 连接到 Android PC 上的微型 USB 端口的 BASH 执行 shell 命令。所以,我正在尝试通过我的 Android 应用执行此操作。
请指教
最佳答案
在设备上执行命令时,无需使用 adb shell
作为前缀,您的 java 代码会在设备本身上运行,因此只需将其关闭并运行以下命令即可
content insert --uri content://settings/system --bind name:s:user_rotation --bind value:i:0"
请注意,当您这样做时,您告诉 Android 设备在通过 ADB 连接到它的另一台 Android 设备上执行此命令
关于android - 是否可以从应用程序执行 ADB shell 命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28785411/