下面贴的代码处理机制比较山寨,其实应该使用谷歌的safetyNet API,然而这个东西在国内不一定可以使用,自求多福。
public boolean isDeviceRooted(){
//第一种情况,需要用真机确认。
String buildTags = Build.TAGS;
// if(buildTags!=null && buildTags.contains("test-keys")){
// Log.d("isDeviceRooted1","1");
// return true;
// }
File file = new File("/system/app/Superuser.apk");
if(file.exists()){
Log.d("isDeviceRooted2","2");
return true;
}
if(new ExecShellForRoot().executeCommand(ExecShellForRoot.SHELL_CMD.check_su_binary)!=null){
Log.d("isDeviceRooted3","3");
return true;
}
return false;
}
public class ExecShellForRoot {
private static String LOG_TAG = ExecShellForRoot.class.getName();
public static enum SHELL_CMD {
check_su_binary(new String[]{"/system/xbin/which", "su"}),;
String[] command;
SHELL_CMD(String[] command) {
this.command = command;
}
}
public ArrayList<String> executeCommand(SHELL_CMD shellCmd) {
String line = null;
ArrayList<String> fullResponse = new ArrayList<>();
Process localProcess = null;
try {
localProcess = Runtime.getRuntime().exec(shellCmd.command);
} 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) {
LogUtility.debug(ExecShellForRoot.class, "execeuteCommand", "Line received:" + line);
fullResponse.add(line);
}
} catch (IOException e) {
e.printStackTrace();
}
return fullResponse;
}
}