美文网首页Android
Andriod判断手机是否root

Andriod判断手机是否root

作者: 祝小年 | 来源:发表于2016-07-20 10:33 被阅读534次

    Checks if the device is rooted.
    判断手机是否被root

       /**
         * Checks if the device is rooted.
         *
         * @return <code>true</code> if the device is rooted, <code>false</code> otherwise.
         */
        public static boolean isRooted() {
    
            // get from build info
            String buildTags = android.os.Build.TAGS;
            if (buildTags != null && buildTags.contains("test-keys")) {
                return true;
            }
    
            // check if /system/app/Superuser.apk is present
            try {
                File file = new File("/system/app/Superuser.apk");
                if (file.exists()) {
                    return true;
                }
            } catch (Exception e1) {
                // ignore
            }
    
            // try executing commands
            return canExecuteCommand("/system/xbin/which su")
                    || canExecuteCommand("/system/bin/which su") || canExecuteCommand("which su")
                    || canExecuteCommand("busybox which su");
        }
    
        // executes a command on the system
        private static boolean canExecuteCommand(String command) {
            Process process = null;
            try {
                process = Runtime.getRuntime().exec(command);
                BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
                String info = in.readLine();
                if (info != null) return true;
                return false;
            } catch (Exception e) {
                //do noting
            } finally {
                if (process != null) process.destroy();
            }
            return false;
        }
    

    相关文章

      网友评论

      • 磨剑者:在网上找了很多判断root权限的代码都不能适配所有的机型,用博主的方法,没有发现问题。大赞!!!

      本文标题:Andriod判断手机是否root

      本文链接:https://www.haomeiwen.com/subject/apjvjttx.html