首页 > 移动平台 > 详细

android请求root权限

时间:2015-05-05 14:10:37      阅读:201      评论:0      收藏:0      [点我收藏+]

应用获取Root权限的原理:让应用的代码执行目录获取最高权限。在Linux中通过chmod 777 [代码执行目录]

 

//请求root权限
    public static boolean upgradeRootPermission(String pkgCodePath) {  
        Process process = null;  
        DataOutputStream os = null;  
        Boolean resBoolean;
        try {  
            String cmd="chmod 777 " + pkgCodePath;  
            process = Runtime.getRuntime().exec("su"); //切换到root帐号  
            os = new DataOutputStream(process.getOutputStream());  
            os.writeBytes(cmd + "\n");  
            os.writeBytes("exit\n");  
            os.flush();  
            resBoolean= process.waitFor()==0;  
        } catch (Exception e) {  
            return false;  
        } finally {  
            try {  
                if (os != null) {  
                    os.close();  
                }  
                process.destroy();  
            } catch (Exception e) {  
            }  
        }  
        return resBoolean;  
    }  
   

 

//调用函数

if(upgradeRootPermission(getPackageCodePath()))
        {
            Toast.makeText(MainActivity.this, "Root 权限请求成功", Toast.LENGTH_SHORT).show();
        }
        else{
            Toast.makeText(MainActivity.this, "Root 权限请求失败,无法使用!程序将自动退出!", Toast.LENGTH_SHORT).show();
            finish();
        }

android请求root权限

原文:http://www.cnblogs.com/lzh-Linux/p/4478793.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!