執行單行命令:
/**
* 執行Android命令
*
* @param cmd 命令
*/
public static String execSuCmd(String cmd) {
String out = "";
Process process = null;
DataOutputStream os = null;
DataInputStream is = null;
try {
process = Runtime.getRuntime().exec("su");
os = new DataOutputStream(process.getOutputStream());
os.writeBytes(cmd + "\n");
os.writeBytes("exit\n");
os.flush();
int aa = process.waitFor();
is = new DataInputStream(process.getInputStream());
byte[] buffer = new byte[is.available()];
is.read(buffer);
out = new String(buffer);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (os != null) {
os.close();
}
if (is != null) {
is.close();
}
if (process != null) {
process.destroy();
}
} catch (Exception e) {
}
}
return out;
}
多行命令:
ArrayList<String> commandList = new ArrayList<String>();
commandList.add("reboot");
ExecuteAsRoot executeAsRoot = new ExecuteAsRoot(commandList);
executeAsRoot.execute();
public class ExecuteAsRoot extends AExecuteAsRoot {
ArrayList<String> list;
public ExecuteAsRoot(ArrayList<String> list) {
this.list = list;
}
@Override
protected ArrayList<String> getCommandsToExecute() {
return list;
}
}
public abstract class AExecuteAsRoot {
public static boolean canRunRootCommands() {
boolean retval = false;
Process suProcess;
try {
suProcess = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());
DataInputStream osRes = new DataInputStream(suProcess.getInputStream());
if (null != os && null != osRes) {
// Getting the id of the current user to check if this is root
os.writeBytes("id\n");
os.flush();
String currUid = osRes.readLine();
boolean exitSu = false;
if (null == currUid) {
retval = false;
exitSu = false;
Log.d("ROOT", "Can't get root access or denied by user");
} else if (true == currUid.contains("uid=0")) {
retval = true;
exitSu = true;
Log.d("ROOT", "Root access granted");
} else {
retval = false;
exitSu = true;
Log.d("ROOT", "Root access rejected: " + currUid);
}
if (exitSu) {
os.writeBytes("exit\n");
os.flush();
}
}
} catch (Exception e) {
// Can't get root !
// Probably broken pipe exception on trying to write to output
// stream after su failed, meaning that the device is not rooted
retval = false;
Log.d("ROOT",
"Root access rejected [" + e.getClass().getName() + "] : " + e.getMessage());
}
return retval;
}
public final boolean execute() {
boolean retval = false;
try {
ArrayList<String> commands = getCommandsToExecute();
if (null != commands && commands.size() > 0) {
Process process = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
for (String currCommand : commands) {
os.writeBytes(currCommand + "\n");
os.flush();
}
os.writeBytes("exit\n");
os.flush();
BufferedReader reader = new BufferedReader(new InputStreamReader(
process.getInputStream()));
int read;
char[] buffer = new char[4096];
StringBuffer output = new StringBuffer();
while ((read = reader.read(buffer)) > 0) {
output.append(buffer, 0, read);
}
reader.close();
try {
int suProcessRetval = process.waitFor();
if (255 != suProcessRetval) {
retval = true;
} else {
retval = false;
}
System.out.println("BBBB: " + output.toString());
} catch (Exception ex) {
//Log.e("Error executing root action", ex);
}
}
} catch (IOException ex) {
Log.w("ROOT", "Can't get root access", ex);
} catch (SecurityException ex) {
Log.w("ROOT", "Can't get root access", ex);
} catch (Exception ex) {
Log.w("ROOT", "Error executing internal operation", ex);
}
return retval;
}
protected abstract ArrayList<String> getCommandsToExecute();
}