`

android代码库之执行Shell命令或者脚本

阅读更多
public String execCommand(String command) throws IOException {
// start the ls command running
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(command); // 这句话就是shell与高级语言间的调用
// 如果有参数的话可以用另外一个被重载的exec方法
// 实际上这样执行时启动了一个子进程,它没有父进程的控制台
// 也就看不到输出,所以我们需要用输出流来得到shell执行后的输出
InputStream inputstream = proc.getInputStream();
InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
// read the ls output
String line = “”;
StringBuilder sb = new StringBuilder(line);
while ((line = bufferedreader.readLine()) != null) {
// System.out.println(line);
sb.append(line);
sb.append(‘\n’);
}
// 使用exec执行不会等执行成功以后才返回,它会立即返回
// 所以在某些情况下是很要命的(比如复制文件的时候)
// 使用wairFor()可以等待命令执行完成以后才返回
try {
if (proc.waitFor() != 0) {
System.err.println(“exit value = ” + proc.exitValue());
}
} catch (InterruptedException e) {
System.err.println(e);
}
return sb.toString();
}
另一段执行Shell的代码
分享到:
评论
1 楼 Trinea 2014-02-28  
关于Android中利用Shell执行某些命令已封装成完善的API,可轻松实现如修改hosts,拷贝文件、安装apk及在拥有root权限下执行某些操作等,如拷贝文件如下:

String[] commands = new String[] { "mount -o rw,remount /system", "cp /mnt/sdcard/xx.apk /system/app/" };
CommandResult result = ShellUtils.execCommand(commands, true);

具体可以看看这篇文章介绍 http://www.trinea.cn/android/android-java-execute-shell-commands/

相关推荐

Global site tag (gtag.js) - Google Analytics