需要导入的类:
import java.io.BufferedReader;
import java.io.InputStreamReader;
方法体:
/**
* 执行shell 命令, 命令中不必再带 adb shell
* @param cmd
* @return Sting 命令执行在控制台输出的结果
*/
public static String execByRuntime(String cmd) {
Process process = null;
BufferedReader bufferedReader = null;
InputStreamReader inputStreamReader = null;
try {
process = Runtime.getRuntime().exec(cmd);
inputStreamReader = new InputStreamReader(process.getInputStream());
bufferedReader = new BufferedReader(inputStreamReader);
int read;
char[] buffer = new char[4096];
StringBuilder output = new StringBuilder();
while ((read = bufferedReader.read(buffer)) > 0) {
output.append(buffer, 0, read);
}
return output.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if (null != inputStreamReader) {
try {
inputStreamReader.close();
} catch (Throwable t) {
}
}
if (null != bufferedReader) {
try {
bufferedReader.close();
} catch (Throwable t) {
}
}
if (null != process) {
try {
process.destroy();
} catch (Throwable t) {
}
}
}
}