Skip to content

Instantly share code, notes, and snippets.

@jackuhan
Created March 30, 2017 16:10
Show Gist options
  • Save jackuhan/a84e293ed55dcae335942d7f1f8720a6 to your computer and use it in GitHub Desktop.
Save jackuhan/a84e293ed55dcae335942d7f1f8720a6 to your computer and use it in GitHub Desktop.
RootUtil.java in android
package com.cyjh.elfin.util;
import com.cyjh.mobileanjian.ipc.engine.utils.f;
import com.cyjh.mobileanjian.ipc.utils.CLog;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
public class RootUtil {
private static final String TAG = "ROOT_UTIL";
public static boolean isRoot() {
int i;
String sys = System.getenv("PATH");
ArrayList<String> commands = new ArrayList();
String[] path = sys.split(":");
for (String str : path) {
String filePathName = str + File.separator + f.a;
if (new File(filePathName).exists()) {
commands.add("ls -l " + filePathName);
}
}
ArrayList<String> res = run("/system/bin/sh", commands);
String response = "";
for (i = 0; i < res.size(); i++) {
response = response + ((String) res.get(i));
}
CLog.i(TAG, ">>>>>>>>>>>ls -l 命令执行结果:" + response);
if (response.contains("-rws")) {
return true;
}
return execSuCmd();
}
private static boolean execSuCmd() {
boolean rootStatus;
Throwable th;
Process process = null;
DataOutputStream os = null;
try {
process = Runtime.getRuntime().exec(f.a);
DataOutputStream os2 = new DataOutputStream(process.getOutputStream());
try {
os2.writeBytes(f.c);
os2.flush();
process.waitFor();
CLog.i(TAG, ">>>>>>>设备有root权限,但用户主动拒绝了,表示设备有root");
rootStatus = true;
if (os2 != null) {
try {
os2.close();
} catch (Exception e) {
os = os2;
}
}
process.destroy();
os = os2;
} catch (Exception e2) {
os = os2;
try {
CLog.i(TAG, ">>>>>>>执行su命令异常,表示设备未root");
rootStatus = false;
if (os != null) {
try {
os.close();
} catch (Exception e3) {
}
}
process.destroy();
return rootStatus;
} catch (Throwable th2) {
th = th2;
if (os != null) {
try {
os.close();
} catch (Exception e4) {
throw th;
}
}
process.destroy();
throw th;
}
} catch (Throwable th3) {
th = th3;
os = os2;
if (os != null) {
os.close();
}
process.destroy();
throw th;
}
} catch (Exception e5) {
CLog.i(TAG, ">>>>>>>执行su命令异常,表示设备未root");
rootStatus = false;
if (os != null) {
os.close();
}
process.destroy();
return rootStatus;
}
return rootStatus;
}
private static ArrayList<String> run(String shell, ArrayList<String> commands) {
ArrayList<String> 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()));
Iterator i$ = commands.iterator();
while (i$.hasNext()) {
String command = (String) i$.next();
CLog.i(TAG, "command: " + command);
shellInput.write((command + " 2>&1\n").getBytes());
}
shellInput.write(f.c.getBytes());
shellInput.flush();
while (true) {
String line = shellOutput.readLine();
if (line == null) {
break;
}
CLog.i(TAG, "result: " + line);
output.add(line);
}
process.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e2) {
e2.printStackTrace();
} finally {
process.destroy();
}
return output;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment