当前位置:操作系统 > 安卓/Android >>

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

为了方便给出上一篇上一篇地址:  <a href="http://www.zzzyk.com/kf/201203/124585.html > 
调了几天这个root权限获取问题终于搞定了,各种百度谷歌,各种方法全部都测试过终于有眉目了
我通过这几天测试总结了三个方法获取root权限问题:
1 上一篇文章所引用的方法
[html]
</pre><pre>  
[html]
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){//<strong><span style="font-size:18px;color:#ff0000;">说明是NULL。。。。就是不能访问其下的文件了 
</span></strong>           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; 
    } 
 

[java]
<span style="font-family:Arial;BACKGROUND-COLOR: #ffffff"></span>其中我这里是把dos.writeBytes("exit\n");去掉了,发现手机上提示获取权限成功,但是问题来了,手机黑屏,程序还在运行,就是黑屏。。。。。等下还会跳出是否强制关闭,这个原因下面或解释 
[java]
<strong><span style="font-size:18px;"><span style="color:#3333ff;">2 <span class="link_title"><a title="RootExplorer怎么样获取root权限的" href="http://blog.csdn.net/a345017062/article/details/6441986">RootExplorer获取root权限的</a>方法(以下是来自RootExplorer的源码)</span></span></span></strong> 
[html]
<span style="font-size:18px;"><strong><span style="BACKGROUND-COLOR: #000000">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);   
</span>}  </strong></span> 
[java]
经过我的测试也是可行的,不过问题还是一样的,就是黑屏,还会时而跳出是否强制关闭程序。 
[java]
<span style="font-size:18px;"><strong>3 来自谷歌 </strong></span><a href="http://code.google.com/p/superuser/"><span style="font-size:18px;"><strong>http://code.google.com/p/su
补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,