gpt4 book ai didi

java - 在不同平台上从 Java 代码运行 adb shell 命令

转载 作者:行者123 更新时间:2023-11-30 00:46:12 25 4
gpt4 key购买 nike

我正在使用 Mac 运行 Java 程序,其中包含一些要在远程 Android 设备上执行的命令。当我在 Windows 机器上执行我的程序时,它会提供正确的输出,但是当我在 Mac 机器上执行相同的程序时,它不会。我在这里分享代码 fragment 和我得到的输出。

代码:

 private static final String DUMPSYSCOMMAND = "adb -s ? shell \"dumpsys package com.PACKAGENAME.service | grep versionName\"";

String versionString = runADBCommand(DUMPSYSCOMMAND.replace("?",
deviceIP));
System.out.println("Version String Result " + versionString);

String versionName = null;
if (versionString != null && !versionString.isEmpty()) {
versionString = versionString.replace("\\s+", "");
versionName = versionString.replace(".", "-")
.substring(versionString.indexOf("=") + 1)
.replaceAll("\\s+", "");
System.out.println("Version String " + versionName);

}

public String runADBCommand(String adbCommand) throws IOException {
System.out.println("Running given command= " + adbCommand + "$$$");
StringBuffer returnValue = new StringBuffer();
String line;
InputStream inStream = null;
try {
System.out.println("adbCommand = " + adbCommand);
Process process = Runtime.getRuntime().exec(adbCommand);

// process.waitFor();/
inStream = process.getInputStream();
BufferedReader brCleanUp = new BufferedReader(
new InputStreamReader(inStream));
while ((line = brCleanUp.readLine()) != null) {
if (!line.equals("")) {
System.out.println("After exec");
System.out.println("Line=" + line);

}

// returnValue = returnValue + line + "\n";
returnValue.append(line).append("\n");
}

brCleanUp.close();
try {


process.waitFor();

} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("Error: " + e.getMessage());
}
System.out.println(returnValue.toString() + "@@");
return returnValue.toString();
}

输出:

@@Running given command= adb -s DEVICEIP shell "dumpsys package PACKAGENAME | grep versionName"$$$
adbCommand = adb -s DEVICEIP shell "dumpsys package PACKAGENAME | grep versionName"
After exec
Line=/system/bin/sh: dumpsys package PACKAGENAME | grep versionName: not found
/system/bin/sh: dumpsys package PACKAGENAME | grep versionName: not found
@@Version String Result /system/bin/sh: dumpsys package PACKAGENAME | grep versionName: not found
Version String /system/bin/sh:dumpsyspackagecom-PACKAGENAME|grepversionName:notfound

当我从命令提示符运行相同的 shell 命令时,它也会在 Mac 上为我提供预期的输出。

最佳答案

最好改用 ProcessBuilder。但是如果你坚持使用 Runtime.getRuntime().exec() - 使用 .exec(String[] cmdarray) 而不是你当前的 .exec(String命令):

private static final String DUMPSYSCOMMAND = "dumpsys package com.PACKAGENAME.service | grep versionName";

String versionString = runADBCommand({"adb", "-s", deviceIP, "shell", DUMPSYSCOMMAND});

...

public String runADBCommand(String[] adbCommand) throws IOException {

...

// do not forget to remove / modify this println - it expect a string
// System.out.println("adbCommand = " + adbCommand);

关于java - 在不同平台上从 Java 代码运行 adb shell 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41764909/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com