1.通过检测指定目录下是否存在su程序来检测运行环境是否为Root设备
public static boolean CheckRootPathSU() {
File f = null;
final String kSuSearchPaths[] = {"/system/bin/", "/system/xbin/", "/system/sbin/", "/sbin/", "/vendor/bin/"};
try {
for (int i = 0; i < kSuSearchPaths.length; i++) {
f = new File(kSuSearchPaths[i] + "su");
if (f != null && f.exists()) {
return true;
}
}
}catch(Exception e)
{
e.printStackTrace();
}
return false;
}
当CheckRootPathSU返回值为true时,禁止应用启动
2、或者通过which命令检测系统PATH变量指定的路径下是否存在su程序来检测运行环境是否为Root设备
public static boolean checkRootWhichSU() {
String[] strCmd = new String[]{"/system/xbin/which", "su"};
ArrayList<String> execResult = executeCommand(strCmd);
if (execResult != null) {
return true;
} else {
return false;
}
}
public static ArrayList<String> executeCommand(String[] shellCmd) {
String line = null;
ArrayList<String> fullResponse = new ArrayList<String>();
Process localProcess = null;
try {
localProcess = Runtime.getRuntime().exec(shellCmd);
} catch (Exception e) {
return null;
}
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(localProcess.getOutputStream()));
BufferedReader in = new BufferedReader(new InputStreamReader(localProcess.getInputStream()));
try {
while ((line = in.readLine()) != null) {
fullResponse.add(line);
}
} catch (Exception e) {
e.printStackTrace();
}
return fullResponse;
}
当CheckRootWhichSU返回值为true时,禁止应用启动