android - 是否可以从应用程序执行 ADB shell 命令?

标签 android shell adb screen-orientation

我有一个 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/

相关文章:

android - 如果我在Android中使用Telegram api,是否需要服务器?

android - RecyclerView 未显示在布局中

android - 有没有办法在 Android Studio 3.6 canary 9、10 中显示 float 布局预览窗口

java - 如何从 fragment 中的工具栏在 ListView 中搜索

shell - xargs 执行字符串 - 我做错了什么?

shell - 使用 shell 和 curl 检查站点是否可用以进行 https URL 重定向

android - adb 套接字不工作和守护进程

linux - 列出所有文件并找到特定文件位置

android - 'adb' 不是内部或外部命令、可运行程序或批处理文件

android - 如何在 Windows 8 上调试 Samsung Galaxy S Advanced 上的 Android 应用程序?