exec shell command - Android Android OS

Android examples for Android OS:Shell Command

Description

exec shell command

Demo Code


//package com.java2s;

import android.util.Log;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

public class Main {
    private static final String TAG = "CommandUtils";

    public static int exec(boolean root, boolean waitFor, String command) {
        return exec(root, waitFor, command, null, null);
    }/* w w w . ja v  a  2s.c  o m*/

    public static int exec(boolean root, boolean waitFor, String[] commands) {
        return exec(root, waitFor, commands, null, null);
    }

    public static int exec(boolean root, boolean waitFor, String command,
            StringBuilder out, StringBuilder err) {
        return exec(root, waitFor, new String[] { command }, out, err);
    }

    public static int exec(boolean root, boolean waitFor,
            String[] commands, StringBuilder out, StringBuilder err) {
        Log.d(TAG, "exec root: " + root + ", waitFor: " + waitFor
                + ", command = " + Arrays.toString(commands));
        Runtime runtime = Runtime.getRuntime();
        Process process = null;
        int exitValue = -1;
        if (commands == null) {
            return -1;
        }

        try {
            if (root) {
                process = runtime.exec("su\n");
                for (String command : commands) {
                    process.getOutputStream().write(
                            (command + "\n").getBytes());
                    process.getOutputStream().flush();
                }
                process.getOutputStream().write("exit\n".getBytes());
            } else {
                process = runtime.exec(commands);
            }

            process.getOutputStream().write("exit\n".getBytes()); //??process
            if (waitFor) {
                process.waitFor();//??process?????
            }

            byte[] outData = getData(process.getInputStream());//????????
            if (out != null) {
                out.append(new String(outData));
            }

            byte[] errorData = getData(process.getErrorStream());//????????
            if (err != null) {
                err.append(new String(errorData));
            }

            while (true) {
                try {
                    exitValue = process.exitValue();
                    break;
                } catch (Exception e) {
                    // TODO: handle exception
                    Thread.sleep(100);
                    Log.d(TAG, "exec wait to get exitValue!");
                }
            }

        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            Log.e(TAG, e.toString());
        } finally {
            if (process != null) {
                process.destroy();
            }
        }
        Log.d(TAG, "exitValue: " + exitValue + ", out: "
                + (out == null ? "" : out.toString()) + ", err: "
                + (err == null ? "" : err.toString()));
        return exitValue;
    }

    private static byte[] getData(InputStream is) throws IOException {
        DataInputStream dis = new DataInputStream(is);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int read = -1;
        byte[] buffer = new byte[1024];
        while ((read = dis.read(buffer)) > 0) {
            baos.write(buffer, 0, read);
            Arrays.fill(buffer, 0, buffer.length, (byte) 0);
        }
        baos.close();
        return baos.toByteArray();
    }
}

Related Tutorials