申请root权限

检测是否有root

    public static boolean checkRooted() {
        boolean result = false;
        try {
            result = new File("/system/bin/su").exists() || new File("/system/xbin/su").exists();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

 

 

 

 

 1 public boolean getRootPermission(String pkgCodePath) {
 2         Log.d(TAG, "pkgCodePath=" + pkgCodePath);
 3         Process process = null;
 4         DataOutputStream os = null;
 5         try {
 6             String cmd = "chmod 777 " + pkgCodePath;
 7             process = Runtime.getRuntime().exec("su"); //切换到root帐号
 8             os = new DataOutputStream(process.getOutputStream());
 9             os.writeBytes(cmd + "\n");
10             os.writeBytes("exit\n");
11             os.flush();
12             process.waitFor();
13         } catch (Exception e) {
14             Toast.makeText(this, "root error!" + pkgCodePath, Toast.LENGTH_SHORT).show();
15             e.printStackTrace();
16             return false;
17         } finally {
18             try {
19                 if (os != null) {
20                     os.close();
21                 }
22                 process.destroy();
23 
24 
25             } catch (Exception e) {
26             }
27         }
28         Toast.makeText(this, "root success!" + pkgCodePath, Toast.LENGTH_SHORT).show();
29         return true;
30     }
getRootPermission

然后在 想申请root的地方   执行方法  

getRootPermission(getPackageCodePath());


静默安装 卸载

参考
http://www.2cto.com/kf/201503/381170.html
http://ju.outofmemory.cn/entry/108796


posted @ 2016-06-03 11:58  demon9  阅读(562)  评论(0编辑  收藏  举报