String cmd = "ls"; //命令
//Runtime对象
Runtime runtime = Runtime.getRuntime();
try {
Process process = runtime.exec(cmd);
//获得结果的输入流
InputStream input = process.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(input));
String strLine;
while(null != (strLine = br.readLine())){
System.out.println(strLine);
}
} catch (IOException e) {
e.printStackTrace();
}
运行后将在LogCat中打印出 >ls 命令的结果。
注:有的命令需要Root权限,如果权限不足或有异常出现。