Handy Android code snippets
This blog post is a memo of handy Android code snippets that I found particularly useful in programming and hacking.
Execute a shell command as root
Reference: https://blog.csdn.net/black_bird_cn/article/details/79717245
public static String execRootCmd(String cmd) {
String result = "";
DataOutputStream dos = null;
DataInputStream dis = null;
try {
Process p = Runtime.getRuntime().exec("su");
dos = new DataOutputStream(p.getOutputStream());
dis = new DataInputStream(p.getInputStream());
dos.writeBytes(cmd + "\n");
dos.flush();
dos.writeBytes("exit\n");
dos.flush();
String line = null;
while ((line = dis.readLine()) != null) {
result += line;
}
p.waitFor();
}
catch (Exception e) {
}
finally {
if (dos != null) {
try { dos.close(); }
catch (IOException e) { }
}
if (dis != null) {
try { dis.close(); }
catch (IOException e) {
}
}
return result;
}
Leave a Comment
Your email address will not be published. Required fields are marked *