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

Android编程获取手机root权限问题

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

最近再写一个文件管理器,需要访问到root权限才能访问的文件夹,  虽说获取权限成功,但是还是不能访问到需root权限才能访问的文件夹,同样的问题我同学写的一模一样的获取权限方法,他就可以,奇怪。。  我放到已经root过的手机上测试,没提示说要获取root,直接就报错了。。。。
 
今天改了一下,把dos.writeBytes("exit\n");去掉了,发现手机上提示获取权限成功,但是问题来了,手机黑屏,程序还在运行,就是黑屏。。。。。等下还会跳出是否强制关闭。。。这句话是啥意思呢????谢谢各位给点建议

 求解释啊。。。。。。 


摘自  魏艺荣的专栏 
补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,