自己的手机是LG的NEXUS4,用过了网上各种代码判断的方法。 运行后都显示是ROOT过。但手机本身实际上并没有ROOT过。 但是使用其他一款软件的时候可以正确判断出我的手机有没ROOT过。 不知道问题到底出在哪里。 希望有高人可以指点下。。非常感谢。。
public static boolean isRoot() {
boolean isRoot = false;
String sys = System.getenv("PATH");
ArrayList commands = new ArrayList();
String[] path = sys.split(":");
for (int i = 0; i < path.length; i++) {
String commod = "ls -l " + path[i] + "/su";
commands.add(commod);
System.out.println("commod : "+commod);
}
ArrayList res = run("/system/bin/sh", commands);
String response = "";
for (int i = 0; i < res.size(); i++) {
response += res.get(i);
}
String root = "-rwsr-sr-x root root";
if (response.contains(root)) {
isRoot = true;
}
return isRoot;
}
public static ArrayList run(String shell, ArrayList commands){
ArrayList output = new ArrayList();
Process process =null;
try {
process = Runtime.getRuntime().exec(shell);
BufferedOutputStream shellInput = new BufferedOutputStream(
process.getOutputStream());
BufferedReader shellOutput = new BufferedReader(
new InputStreamReader(process.getInputStream()));
for (String command : commands) {
shellInput.write((command + " 2>&1\n").getBytes());
}
shellInput.write("exit\n".getBytes());
shellInput.flush();
String line;
while ((line = shellOutput.readLine()) != null) {
output.add(line);
}
process.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}finally{
process.destroy();
}
return output;
}
这代码看你用过没