【问题标题】:Is it possible to execute ADB shell commands from app?是否可以从应用程序执行 ADB shell 命令?
【发布时间】:2015-05-01 08:46:44
【问题描述】:

我有一个带有 root(开箱即用)的Android PC 连接到始终以横向显示的外接显示器(HDMI 和 USB),即使我的应用程序在 Activity 中指定了纵向Manifest 中的声明:

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 中枚举设备,也无法从 BASH 执行 shell 命令,将我的开发 PC 连接到 Android PC 上的微型 USB 端口。所以,我正在尝试从我的 Android 应用程序中执行此操作。

请指教

【问题讨论】:

    标签: android shell adb screen-orientation


    【解决方案1】:

    在设备上执行命令时,无需在其前面加上 adb shell,您的 java 代码在设备本身上运行,因此只需将其删除并运行以下命令即可

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

    简单说明一下,当你这样做时,你告诉 Android 设备在另一个通过 ADB 连接到它的 Android 设备上执行此命令

    【讨论】:

    • 你说得对,adb 命令不在本地执行,而是直接调用命令以调用应用程序的用户 ID 运行它,这对于大多数这些事情来说可能是不够的。这就是为什么做这种事情的人倾向于使用su root shim。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-06
    • 1970-01-01
    • 2019-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多