Android程序获取root权限问题的最终解决与分析

来源:互联网 发布:电脑软件管理 编辑:程序博客网 时间:2024/05/22 15:46
      为了方便给出上一篇上一篇地址:  http://blog.csdn.net/up1up2up3/article/details/7380651,调了几天这个root权限获取问题终于搞定了,各种百度谷歌,各种方法全部都测试过终于有眉目了

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

1 、上一篇文章所引用的方法

public class DemoActivity extends Activity {public final String rootPowerCommand = "chmod 777 /dev/block/mmcblk0";// 授权root权限命令    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);       new AlertDialog.Builder(this).setMessage(rootCommand(rootPowerCommand)+"....").show();       File []files =  new File("/root").listFiles();       if(files==null){//说明是NULL。。。。就是不能访问其下的文件了                 new AlertDialog.Builder(this).setMessage(".OK...").show();       }      // files[0].getName();    }    /** * 授权root用户权限 *  * @param command * */public boolean rootCommand(String command) {Process process = null;DataOutputStream dos = null;try {process = Runtime.getRuntime().exec("su");dos = new DataOutputStream(process.getOutputStream());dos.writeBytes(command + "\n");dos.writeBytes("exit\n");dos.flush();process.waitFor();} catch (Exception e) {return false;} finally {try {if (dos != null) {dos.close();}process.destroy();} catch (Exception e) {}}return true;}}
其中我这里是把dos.writeBytes("exit\n");去掉了,发现手机上提示获取权限成功,但是问题来了,手机黑屏,程序还在运行,就是黑屏。。。。。等下还会跳出是否强制关闭,这个原因下面或解释

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 、来自谷歌 http://code.google.com/p/superuser/,(关于Superuser超级管理器大伙自个百度之),下面是他的获取root权限源码

 

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

这种方法我测试了下,没办法,估计还要改一些地方,不推荐使用,当然可以研究研究呵呵。

分析及解决问题:

现在,来说明下为什么会黑屏现象,从以上描述我们可以知道程序一定在运行!!!!只是为什么会显示不了呢?关键点就在这,我们想想以前学习javaSE时写的线程啊呵呵,不是在写界面与线程方面的学习时,经常碰到一按按钮(Button)界面就卡死吗?我自己来说吧,在做一个类QQ的应用程序时,服务器弄了个按钮,点击就启动服务器,可是一点击他就卡死了,虽说服务器起来了,可是关不掉啊,服务器启动和关闭界面卡死了(就跟Android的那个黑屏、显示强制关闭一样呵呵),当时显然的就联想到了那个线程方法来启动服务器啊!!  所以现在这种思想也应该继续保持哦 - -!    接下去咱不就不解释了,大伙懂的!弄个线程。。解决之。。。。。
     这个问题调试了n久,也弄了n种方法,模拟器又没root(光拔插手机调试就不知道弄了几次。。。 - -#),还有什么不足之处,大伙多多补充啊!!Good good Study,day day up! - - !