安卓(Android) 怎么判断是否有 Root

2019年10月13日14:53:21安卓(Android) 怎么判断是否有 Root已关闭评论 378 830字阅读2分46秒

1.简介

安卓(Android) 怎么判断是否有 Root?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */



import java.io.File;

public class Main {
    private static final String SU_PATH = "/system/bin/su";
    private static final String SU_PATH_X = "/system/xbin/su";
    private static final String APP_PATH = "/system/app/";

    
    public static int hasRoot() {
        boolean hasSU = findSU();
        if (!hasSU) {
            return 0;
        }
        boolean hasSuperUser = findSuperUser();
        return hasSuperUser ? 2 : 1;
    }

    private static boolean findSU() {
        boolean ret = openFile(SU_PATH).exists();
        if (!ret) {
            ret = openFile(SU_PATH_X).exists();
        }
        return ret;
    }

    private static boolean findSuperUser() {
        File apps = new File(APP_PATH);
        String[] apks = apps.list();
        boolean hasSuperUser = false;
        if (apks != null) {
            if (apks.length > 0) {
                for (String apk : apks) {
                    if (apk.toLowerCase().contains("superuser.apk")) {
                        hasSuperUser = true;
                        break;
                    }
                }
            }
        }
        return hasSuperUser;
    }

    private static File openFile(String path) {
        return new File(path);
    }
}

 
继续阅读
编程十万个怎么办