【问题标题】:adb shell command from android app来自 Android 应用程序的 adb shell 命令
【发布时间】:2018-06-05 05:47:03
【问题描述】:

当我尝试运行以下内容时

process = Runtime.getRuntime().exec("input touchscreen swipe 100 1000 300 1000 1000"); //Normal swipe 有效。

但是,当我按以下方式使用它时,它不起作用。
String[] inputs = {"adb", "shell", "input touchscreen swipe 500 1000 600 1000 1000"}; Process p = Runtime.getRuntime().exec(inputs); p.waitFor();

我有另一个要运行的命令,要运行它,我必须使用第二种方法。谁能告诉我是什么原因或者我怎样才能让第二个运行?

【问题讨论】:

  • 它抛出了什么错误?
  • @nakul 它不会抛出任何错误。它只是不起作用。

标签: java android shell adb


【解决方案1】:
Process p = Runtime.getRuntime().exec(inputs);

抛出几个异常,包括但不限于 IO、Null Pointer、Index Out of Bound。

尝试用 try catch 包围该行

Process p;
    try {
        p = Runtime.getRuntime().exec(ch);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

【讨论】:

  • 我已经指定了确切的 sn-p 以保持上下文清晰。捕获块已经到位。谢谢..
【解决方案2】:

您应该粘贴错误内容(如果存在)。 (例如,在 logcat 中。)

adb 是客户端,它可能不存在于您的 Android 设备中(自 Android 5.0+ 起)。您不应在 Android 应用命令中使用它。对于您的示例,您可以直接使用input 命令而不使用adb

还有,

process = Runtime.getRuntime().exec("input touchscreen swipe 100 1000 300 1000 1000");

等价于

String[] inputs = {"input", "touchscreen", "swipe", "100", "1000", "300", "1000", "1000"}; process = Runtime.getRuntime().exec(inputs);.

请注意您的问题中inputs 中删除的前两个元素。

【讨论】:

  • 没有错误。它只是不起作用....正如我在问题中提到的,第一个工作顺利且良好。第二个没有。更改为您建议的内容后,它似乎只创建了一个事件(可能是关闭),然后保持锁定,从而导致 ANR。
  • @grad9 在我的测试中(Android 6.0.1 上的 AIDE 3.2.161123、MIUI 8 7.3.23),我在答案中成功运行了这两个代码,都出现了预期的现象。不知道是不是我的手机有错误(编译缓存,构建缓存等),还是你的代码(编码错误等)。
  • 我使用的是 AIDE Android Studio 3.1.2,Build #AI-173.4720617 && Huawei CRR-L09, 5.1.1。也许您会发现该解决方案不适用于其他设备/操作系统版本等。该示例谈到了链式命令和链式 args ..
  • 在相关问题中找到stackoverflow.com/questions/28337921/…,这个问题可能重复。
  • 感谢您的帮助。 p.waitFor();必须删除才能使其正常工作。
【解决方案3】:

正如@Geno Chen 提到的,参数需要用双引号引起来。 String[] inputs = {"input", "touchscreen", "swipe", "100", "1000", "300", "1000", "1000"}; 必须删除 p.waitFor(); 才能使其正常工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-06
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 2015-05-01
    • 2019-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多