检测安卓手机是否已经Root

检测安卓手机是否已经Root

/**
 * 反编译支付宝SDK得到的判断root的方法,并增加了对‘/su/bin/’目录的判断
 * Created by cyb on 2016/12/15 0015.
 */

public class SystemInfo {

    public static boolean isRooted(){
        // nexus 5x "/su/bin/"
        String[] paths = { "/system/xbin/", "/system/bin/", "/system/sbin/", "/sbin/", "/vendor/bin/", "/su/bin/" };
        try{
            for(int i = 0; i < paths.length; i++){
                String path = paths[i] + "su";
                if(new File(path).exists()){
                    String execResult = exec(new String[] { "ls", "-l", path });
                    Log.d("cyb", "isRooted=" + execResult);
                    if(TextUtils.isEmpty(execResult) || execResult.indexOf("root") == execResult.lastIndexOf("root")){
                        return false;
                    }
                    return true;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    private static String exec(String[] exec){
        String ret = "";
        ProcessBuilder processBuilder = new ProcessBuilder(exec);
        try {
            Process process = processBuilder.start();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while( (line = bufferedReader.readLine()) != null){
                ret += line;
            }
            process.getInputStream().close();
            process.destroy();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ret;
    }
}

你可能感兴趣的:(Android,安全)