public String execRootCmd(String cmd) {
String result = "";
DataOutputStream dos = null;
DataInputStream dis = null;
try {
Process p = Runtime.getRuntime().exec("su");// 经过Root处理的android系统即有su命令
dos = new DataOutputStream(p.getOutputStream());
dis = new DataInputStream(p.getInputStream());
Log.i(TAG, cmd);
dos.writeBytes(cmd + "\n");
dos.flush();
dos.writeBytes("exit\n");
dos.flush();
String line = null;
BufferedReader br = new BufferedReader(new InputStreamReader(dis));
while ((line = br.readLine()) != null) {
Log.d(TAG, "result:" + line);
result += line;
}
p.waitFor();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (dos != null) {
try {
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (dis != null) {
try {
dis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
Runtime.getRuntime().exec获取root权限,并执行命令
(2)个小伙伴在吐槽
- 很荣幸来访您的博客,留言只是证明我来过!
- 世事无常,但这个博客定能永保辉煌!
