关于前篇Android编程获取root权限问题的最终解决与分析 .

为了方便给出前一篇地址:调了几天这个root权限获取问题终于搞定了,各种百度谷歌,各种方法全部都测试过终于有眉目了.

我通过这几天测试总结了三个方法获取root权限问题:

1 上一篇文章所引用的方法/** * 应用程序运行命令获取 Root权限,设备必须已破解(获得ROOT权限) * * @param command *命令:String apkRoot="chmod 777 "+getPackageCodePath();//<span style="color:#cc0000;"><strong>注意这里的getPackageCodePath()是继承Activity里的方法</strong></span> *RootCommand(apkRoot); * @return 应用程序是/否获取Root权限 */public static boolean RootCommand(String command) {Process process = null;DataOutputStream os = null;try {process = Runtime.getRuntime().exec("su");os = new DataOutputStream(process.getOutputStream());os.writeBytes(command + "\n");os.writeBytes("exit\n");os.flush();process.waitFor();} catch (Exception e) {return false;} finally {try {if (os != null) {os.close();}process.destroy();} catch (Exception e) {}}return true;}这个方法要注意的问题在那条命令chmod 777 getPackageCodePath()有的地方是这样写的chmod 777 /dev/block/mmcblk0

经过几次的测试,我发现后面那一句的话虽然能够成功获取权限,但是!但是运行后的状况是:手机运行,但是程序卡死,时不时的跳出是否强制关闭,我刚开始以为是耗时工作必须放在线程里才不会卡死,就用了异步线程和Thread测试,发现程序仍然卡死,当然有的时候在模拟器上可以运行,而在真机上却卡死也有可能是耗时操作导致,需开线程处理。 但是在这里的情况线程明显不是问题,调了好久,我索性就把权限获取注释掉,发现可以用!运行正常,真机模拟器都可以,这下问题找到了吧!我尝试着把命令改成第一种测试,OK了!!!所有大伙注意这两个的区别。(注:个人认为第二种虽然获取得权限,但是获取权限的进程很快就关闭了,,所以接下去的操作仍有问题,而第一种是获取为此应用程序的获取权限,因此可以正常执行,当然这只是个人见解,有不同看法大家可以指点指点我哈)。

2. RootExplorer获取root权限的方法(以下是来自RootExplorer的源码)

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);

经过我的测试也是可行的,不过问题还是一样的,就是黑屏,还会时而跳出是否强制关闭程序。伤不起3来自谷歌(关于Superuser超级管理器大伙自个百度之),下面是他的获取root权限源码

File superuser = new File("/system/bin/superuser");if (superuser.exists()) {// return device to original stateProcess 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(); }

这种方法我测试了下,没办法,估计还要改一些地方,不推荐使用,当然可以研究研究呵呵,说明一下,这是上一篇的升级版,上一篇的认识还是有误的呵呵,各位多多指点。。。。

public class LinuxShell {public static boolean isRoot(Runtime r, long wait)throws IOException, InterruptedException {boolean root = false;Process p = null;BufferedReader errReader = null;p = Runtime.getRuntime().exec("su");Thread.sleep(wait);errReader = new BufferedReader(new InputStreamReader(p.getErrorStream()));if (!errReader.ready()) {root = true;p.destroy();}return root;}}

同生天地间,为何我不能。

关于前篇Android编程获取root权限问题的最终解决与分析 .

相关文章:

你感兴趣的文章:

标签云: