要使用adb shell命令发送按键事件并返回结果为0,可以使用以下代码示例:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) {
try {
// 执行adb shell命令
Process process = Runtime.getRuntime().exec("adb shell input keyevent KEYCODE_BACK");
// 读取命令输出
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
StringBuilder output = new StringBuilder();
while ((line = reader.readLine()) != null) {
output.append(line).append("\n");
}
// 等待命令执行完成
int exitCode = process.waitFor();
// 输出命令执行结果和返回值
System.out.println("Command output:\n" + output.toString());
System.out.println("Exit code: " + exitCode);
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
}
上述代码使用Java来执行adb shell命令,并读取命令的输出结果和返回值。您需要将代码中的adb shell input keyevent KEYCODE_BACK
替换为您要执行的adb shell命令。
请注意,您需要确保ADB已正确安装,并且设备已通过USB连接到计算机。
上一篇:Android 10 Activity 的 onCreate 崩溃
下一篇:Android 10 创建文件夹 - 使用 getExternalFilesDir() 替代 getExternalStorageDirectory()(已弃用)