ROOT判断

This commit is contained in:
lijia 2022-09-20 15:17:52 +08:00
parent 383f8a9a80
commit 670bf385ac

View File

@ -1,5 +1,6 @@
package com.dahe.mylibrary.utils; package com.dahe.mylibrary.utils;
import android.os.Build;
import android.util.Log; import android.util.Log;
import java.io.BufferedReader; import java.io.BufferedReader;
@ -14,38 +15,28 @@ import java.io.InputStreamReader;
* @Description 判断Android设备是否拥有Root权限 * @Description 判断Android设备是否拥有Root权限
*/ */
public class RootCheck { public class RootCheck {
private final static String TAG = "RootUtil";
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 isRoot() { public static boolean isRoot() {
String binPath = "/system/bin/su"; boolean hasRootDir = false;
String xBinPath = "/system/xbin/su"; String[] rootDirs;
if (new File(binPath).exists() && isExecutable(binPath)) int dirCount = (rootDirs = rootRelatedDirs).length;
return true; for (int i = 0; i < dirCount; ++i) {
if (new File(xBinPath).exists() && isExecutable(xBinPath)) String dir = rootDirs[i];
return true; if ((new File(dir)).exists()) {
return false; hasRootDir = true;
} break;
private static boolean isExecutable(String filePath) {
Process p = null;
try {
p = Runtime.getRuntime().exec("ls -l " + filePath);
// 获取返回内容
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; return Build.TAGS != null && Build.TAGS.contains("test-keys") || hasRootDir;
} }
} }