如何在java.lang.Runtime.getRuntime()中编写adb shell的推荐?
我需要使用adb connect pc到设备并进行一些检查。 所以我尝试使用java.lang.Runtime.getRuntime().exec(cmd)
将adb
shell结果导入我的程序。 但是我不知道如何在exec
调用中编写adb shell,如:
String cmd =adkHome + "adb.exe -s " + device + " shell ls";
然后cd data/app
我该怎么做呢?
这可能是你要找的?
String cmd = "adb shell ls"; String cmdreturn = ""; Runtime run = Runtime.getRuntime(); Process pr = run.exec(cmd); pr.waitFor(); BufferedReader buf = new BufferedReader(new InputStreamReader(pr.getInputStream())); while ((line=buf.readLine())!=null) { System.out.println(cmdreturn); }
String cmd = "adb shell ls"; String cmdreturn = ""; Runtime run = Runtime.getRuntime(); Process pr = run.exec(cmd); pr.waitFor(); BufferedReader buf = new BufferedReader(new InputStreamReader(pr.getInputStream())); while ((line=buf.readLine())!=null) { System.out.println(cmdreturn); }
至于在shell中执行操作,我建议编写一个执行的shell脚本。 在这种情况下,而不是
String cmd = "adb shell ls";
String cmd = "adb shell ls";
替换为
String cmd = "shellscript.sh";
String cmd = "shellscript.sh";
干杯!
如果你的意思是从你的Java应用程序在手机上运行它,你只需要:
String cmd = "ls";