方法1: copy from RootExplorer怎么样获取root权限的
ProcessBuilder pb = new ProcessBuilder("/system/bin/sh");
//java.lang.ProcessBuilder: Creates operating system processes.
pb.directory(new File("/"));//设置shell的当前目录。
try {
Process proc = pb.start();
//获取输入流,可以通过它获取SHELL的输出。
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
BufferedReader err = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
//获取输出流,可以通过它向SHELL发送命令。
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc
.getOutputStream())), true);
out.println("pwd");
out.println("su root");//执行这一句时会弹出对话框(以下程序要求授予最高权限...),要求用户确认。
out.println("cd /data/data");//这个目录在系统中要求有root权限才可以访问的。
out.println("ls -l");//这个命令如果能列出当前安装的APK的数据文件存放目录,就说明我们有了ROOT权限。
out.println("exit");
// proc.waitFor();
String line;
while ((line = in.readLine()) != null) {
System.out.println(line); // 打印输出结果
}
while ((line = err.readLine()) != null) {
System.out.println(line); // 打印错误输出结果
}
in.close();
out.close();
proc.destroy();
} catch (Exception e) {
System.out.println("exception:" + e);
}
方法二:copy from http://code.google.com/p/superuser/
File superuser = new File("/system/bin/superuser");
if (superuser.exists())
{
// return device to original state
Process process = Runtime.getRuntime().exec("superuser");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
os.writeBytes("mount -oremount,rw /dev/block/mtdblock3 /system\n");
os.writeBytes("busybox cp /system/bin/superuser /system/bin/su\n");
os.writeBytes("busybox chown 0:0 /system/bin/su\n");
os.writeBytes("chmod 4755 /system/bin/su\n");
os.writeBytes("rm /system/bin/superuser\n");
os.writeBytes("exit\n");
os.flush();
}