在我的应用程序中,我想运行几个shell命令并解释输出。这些命令本质上是在根手机上运行的on。
我该怎么做呢?
发布于 2011-07-25 05:47:07
首先,确保您需要的shell命令在Android中确实可用。我遇到了一些问题,假设你可以用>来重定向输出。
此方法也适用于我相信v2.2的非根手机,但您应该查看API参考以确保。
try {
Process chmod = Runtime.getRuntime().exec("/system/bin/chmod 777 " +fileName);
BufferedReader reader = new BufferedReader(
new InputStreamReader(nfiq.getInputStream()));
int read;
char[] buffer = new char[4096];
StringBuffer output = new StringBuffer();
while ((read = reader.read(buffer)) > 0) {
output.append(buffer, 0, read);
}
reader.close();
chmod.waitFor();
outputString = output.toString();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}虽然这可能不是100%必需的,但最好让进程等待exec使用process.waitFor()完成,因为您说过您关心输出。
发布于 2011-07-24 03:42:27
您需要首先确保已安装busybox,因为这将安装最常用的shell命令列表,然后使用以下代码运行该命令。
Runtime.getRuntime().exec("ls");https://stackoverflow.com/questions/6802810
复制相似问题