zoukankan      html  css  js  c++  java
  • (五十九)工具方法 ----判断手机是否root和在手机root后获得root权限

    1、判断手机是否root

        public static boolean isPhoneRoot() {
            if ((!new File("/system/bin/su").exists()) && (!new File("/system/xbin/su").exists()) && (!new File("/system/sbin/su").exists()) && (!new File("/sbin/su").exists()) && (!new File("/vendor/bin/su").exists())){
                // System.out.println("手机没有root");
                return false;
            } else {
                // System.out.println("手机root了");
                return true;
            }
        }

    2、在手机root后获得root权限

        /**
         * 应用程序运行命令获取 Root权限,设备必须已破解(获得ROOT权限)
         * 
         * @return 应用程序是/否获取Root权限
         */
        public static boolean upgradeRootPermission(String pkgCodePath) {
             Process process = null;
             DataOutputStream os = null;
             try {
                 if ((!new File("/system/bin/su").exists()) && (!new File("/system/xbin/su").exists()) && (!new File("/system/sbin/su").exists()) && (!new File("/sbin/su").exists()) && (!new File("/vendor/bin/su").exists())){
                     return false;
                 }
                 String cmd="chmod 777 " + pkgCodePath;
                 process = Runtime.getRuntime().exec("su"); //切换到root帐号
                 os = new DataOutputStream(process.getOutputStream());
                 os.writeBytes(cmd + "
    ");
                 os.writeBytes("exit
    ");
                 os.flush();
                 int res = process.waitFor();
    //             DataInputStream in = new DataInputStream(process.getErrorStream());
    //             if(res!=0||in!=null){
    //                 String result = in.readLine();
    //                 System.out.println("in.readLine(): "+result+"   "+res);
    ////                 if("Permission denied".equals(in.readLine())){
    //                 if(result!=null&&result.contains("denied")){
    //                     return false;
    //                 }
    //             }else{
    ////                 System.out.println("in.readLine()==null");
    //             }
                 if(res == 0){
                     return true;
                 }else{
                     return false;
                 }
             } catch (Exception e) {
    //             System.out.println("Exception");
                 e.printStackTrace();
                 return false;
             } finally {
                 try {
                     if (os != null) {
                         os.close();
                     }
                     process.destroy();
                 } catch (Exception e) {
                     e.printStackTrace();
                     return false;
                 }
             }
        }
  • 相关阅读:
    P3332 [ZJOI2013]K大数查询
    树上最短路---------------树链剖分,优化建边。
    BZOJ_4386
    2016_1_13(3)
    2016_1_13(2)
    2016_1_13
    BZOJ_1698
    BZOJ_4152
    BZOJ_3110
    BZOJ_2141
  • 原文地址:https://www.cnblogs.com/fuyanan/p/4440675.html
Copyright © 2011-2022 走看看