Android Utililty Methods Shell Run

List of utility methods to do Shell Run

Description

The list of methods to do Shell Run are organized into topic(s).

Method

booleanexecuteCommand(String command)
execute Command
try {
    Process getRootAccess = Runtime.getRuntime().exec("su");
    DataOutputStream getRootAccessDos = new DataOutputStream(
            getRootAccess.getOutputStream());
    getRootAccessDos.writeBytes(command + "\n");
    getRootAccessDos.writeBytes("exit\n");
    getRootAccessDos.flush();
    getRootAccess.waitFor();
...
StringnormalShell(String normalCommand)
normal Shell
String rOutput = null;
try {
    Process mProcess = Runtime.getRuntime().exec(normalCommand);
    mProcess.waitFor();
    BufferedReader mReader = new BufferedReader(
            new InputStreamReader(mProcess.getInputStream()));
    rOutput = mReader.readLine();
    Log.i(APP, "Output from normalShell = " + rOutput);
...
voidrunAsRoot(String[] commands)
run As Root
Process p = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(p.getOutputStream());
for (String cmd : commands) {
    os.writeBytes(cmd + "\n");
os.writeBytes("exit\n");
os.flush();
os.close();
...
StringrunSimpleCommand(String command, String directory)
Runs a simple command in given directory.
StringBuffer result = new StringBuffer();
log.info("Running simple command " + command + " in " + directory);
Process process = Runtime.getRuntime().exec(command, null,
        new File(directory));
BufferedReader stdout = null;
BufferedReader stderr = null;
try {
    stdout = new BufferedReader(new InputStreamReader(
...
intdoShellCommand(String[] cmds, StringBuilder log, boolean runAsRoot, boolean waitFor)
do Shell Command
Process proc = null;
int exitCode = -1;
if (runAsRoot)
    proc = Runtime.getRuntime().exec("su");
else
    proc = Runtime.getRuntime().exec("sh");
OutputStreamWriter out = new OutputStreamWriter(
        proc.getOutputStream());
...
intfindProcessId(String command)
find Process Id
int procId = -1;
try {
    procId = findProcessIdWithPidOf(command);
    if (procId == -1)
        procId = findProcessIdWithPS(command);
} catch (Exception e) {
    try {
        procId = findProcessIdWithPS(command);
...
intfindProcessIdWithPidOf(String command)
find Process Id With Pid Of
int procId = -1;
Runtime r = Runtime.getRuntime();
Process procPs = null;
String baseName = new File(command).getName();
procPs = r.exec(new String[] { SHELL_CMD_PIDOF, baseName });
BufferedReader reader = new BufferedReader(new InputStreamReader(
        procPs.getInputStream()));
String line = null;
...
intfindProcessIdWithPS(String command)
find Process Id With PS
int procId = -1;
Runtime r = Runtime.getRuntime();
Process procPs = null;
procPs = r.exec(SHELL_CMD_PS);
BufferedReader reader = new BufferedReader(new InputStreamReader(
        procPs.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
...
StringexecRootCmd(String cmd)
exec Root Cmd
String result = "result : ";
try {
    Process p = Runtime.getRuntime().exec("su ");
    OutputStream outStream = p.getOutputStream();
    DataOutputStream dOutStream = new DataOutputStream(outStream);
    InputStream inStream = p.getInputStream();
    DataInputStream dInStream = new DataInputStream(inStream);
    String str1 = String.valueOf(cmd);
...
intexecRootCmdSilent(String cmd)
exec Root Cmd Silent
try {
    Process p = Runtime.getRuntime().exec("su ");
    Object obj = p.getOutputStream();
    DataOutputStream dOutStream = new DataOutputStream(
            (OutputStream) obj);
    String str = String.valueOf(cmd);
    obj = str + "\n";
    dOutStream.writeBytes((String) obj);
...