亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android判斷是否Root方法介紹

 更新時(shí)間:2022年01月23日 11:32:09   作者:Ciruy B.Heimerdinger  
大家好,本篇文章主要講的是Android判斷是否Root方法介紹,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下

為了照顧那些著急的同學(xué),先直接給出結(jié)論:

    private static final String[] rootRelatedDirs = new String[]{
            "/su", "/su/bin/su", "/sbin/su",
            "/data/local/xbin/su", "/data/local/bin/su", "/data/local/su", 
            "/system/xbin/su",
            "/system/bin/su", "/system/sd/xbin/su", "/system/bin/failsafe/su",
            "/system/bin/cufsdosck", "/system/xbin/cufsdosck", "/system/bin/cufsmgr",
            "/system/xbin/cufsmgr", "/system/bin/cufaevdd", "/system/xbin/cufaevdd",
            "/system/bin/conbb", "/system/xbin/conbb"};
 
    public static boolean hasRootPrivilege() {
        boolean hasRootDir = false;
        String[] rootDirs;
        int dirCount = (rootDirs = rootRelatedDirs).length;
        for (int i = 0; i < dirCount; ++i) {
            String dir = rootDirs[i];
            if ((new File(dir)).exists()) {
                hasRootDir = true;
                break;
            }
        }
        return Build.TAGS != null && Build.TAGS.contains("test-keys") || hasRootDir;
    }

好,接下來(lái)我們來(lái)看看到底是如何得到上述的解決方案的。首先,這是既有的判斷root權(quán)限的方案,即判定兩個(gè)root權(quán)限相關(guān)文件夾是否存在,以及當(dāng)前賬戶是否具備訪問(wèn)其內(nèi)容的權(quán)限,如果都成立,那么就認(rèn)為當(dāng)前賬號(hào)具備root權(quán)限。然而,這種root方案在一些情況下不能很好地發(fā)揮作用。

/**
 * 判斷Android設(shè)備是否擁有Root權(quán)限
 */
public class RootCheck {
 
    private final static String TAG = "RootUtil";
 
    public static boolean isRoot() {
        String binPath = "/system/bin/su";
        String xBinPath = "/system/xbin/su";
        if (new File(binPath).exists() && isExecutable(binPath))
            return true;
        if (new File(xBinPath).exists() && isExecutable(xBinPath))
            return true;
        return false;
    }
 
    private static boolean isExecutable(String filePath) {
        Process p = null;
        try {
            p = Runtime.getRuntime().exec("ls -l " + filePath);
            // 獲取返回內(nèi)容
            BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String str = in.readLine();
            Log.i(TAG, str);
            if (str != null && str.length() >= 4) {
                char flag = str.charAt(3);
                if (flag == 's' || flag == 'x')
                    return true;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (p != null) {
                p.destroy();
            }
        }
        return false;
    }
}

然后我就找到了如下方案,該方案號(hào)稱是騰訊bugly的root權(quán)限判斷方案:

private static final String[] a = new String[]{"/su", "/su/bin/su", "/sbin/su",
    "/data/local/xbin/su", "/data/local/bin/su", "/data/local/su", 
    "/system/xbin/su", "/system/bin/su", "/system/sd/xbin/su",
    "/system/bin/failsafe/su", "/system/bin/cufsdosck", 
    "/system/xbin/cufsdosck", "/system/bin/cufsmgr", 
    "/system/xbin/cufsmgr", "/system/bin/cufaevdd", 
    "/system/xbin/cufaevdd", "/system/bin/conbb", 
    "/system/xbin/conbb"};
 
public static boolean p() {
  boolean var0 = false;
  String[] var1 = a;
  int var2 = a.length;
 
  for(int var3 = 0; var3 < var2; ++var3) {
    String var4 = var1[var3];
    if ((new File(var4)).exists()) {
      var0 = true;
      break;
    }
  }
 
  return Build.TAGS != null && Build.TAGS.contains("test-keys") || var0;
}

當(dāng)然,本人生性多疑,偶像是曹操曹丞相,所以自然不能人云亦云,還是實(shí)際確認(rèn)一下bugly實(shí)際上是否是這樣實(shí)現(xiàn)的,以及確保bugly在新的版本中有沒(méi)有對(duì)該方案有進(jìn)一步的改進(jìn)。

然后我就到bugly官網(wǎng),下載了其最新發(fā)布的jar包,筆者下載時(shí)最新的版本為4.4.4,然后直接解壓,然后在解壓的目錄中搜索“test-keys”內(nèi)容。

grep -r test-keys "D:\迅雷下載\Bugly_v3.4.4

最后找到了對(duì)應(yīng)的文件位置和對(duì)應(yīng)方法:com\tencent\bugly\crashreport\common\info\b.class

private static final String[] a = new String[]{"/su", "/su/bin/su",
 "/sbin/su", "/data/local/xbin/su", "/data/local/bin/su", 
"/data/local/su", "/system/xbin/su", "/system/bin/su", 
"/system/sd/xbin/su", "/system/bin/failsafe/su",
 "/system/bin/cufsdosck", "/system/xbin/cufsdosck",
 "/system/bin/cufsmgr", "/system/xbin/cufsmgr",
 "/system/bin/cufaevdd", "/system/xbin/cufaevdd",
 "/system/bin/conbb", "/system/xbin/conbb"};
public static boolean l() {
    boolean var0 = false;
    String[] var1;
    int var2 = (var1 = a).length;
 
    for(int var3 = 0; var3 < var2; ++var3) {
        String var4 = var1[var3];
        if ((new File(var4)).exists()) {
            var0 = true;
            break;
        }
    }
 
    return Build.TAGS != null && Build.TAGS.contains("test-keys") || var0;
}

然后分析一下對(duì)應(yīng)變量的意思,我們就能還原出騰訊判斷Root的代碼,即我們開(kāi)頭所貼出的解決方案:

    private static final String[] rootRelatedDirs = new String[]{
            "/su", "/su/bin/su", "/sbin/su",
            "/data/local/xbin/su", "/data/local/bin/su", "/data/local/su", 
            "/system/xbin/su",
            "/system/bin/su", "/system/sd/xbin/su", "/system/bin/failsafe/su",
            "/system/bin/cufsdosck", "/system/xbin/cufsdosck", "/system/bin/cufsmgr",
            "/system/xbin/cufsmgr", "/system/bin/cufaevdd", "/system/xbin/cufaevdd",
            "/system/bin/conbb", "/system/xbin/conbb"};
 
    public static boolean hasRootPrivilege() {
        boolean hasRootDir = false;
        String[] rootDirs;
        int dirCount = (rootDirs = rootRelatedDirs).length;
        for (int i = 0; i < dirCount; ++i) {
            String dir = rootDirs[i];
            if ((new File(dir)).exists()) {
                hasRootDir = true;
                break;
            }
        }
        return Build.TAGS != null && Build.TAGS.contains("test-keys") || hasRootDir;
    }

到此這篇關(guān)于Android判斷是否Root方法介紹的文章就介紹到這了,更多相關(guān)Android判斷Root內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論