安卓(Android) 怎么通过Shell 获得Root

2019年10月13日15:13:56安卓(Android) 怎么通过Shell 获得Root已关闭评论 448 2549字阅读8分29秒

1.简介

安卓(Android) 怎么通过Shell 获得Root?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */



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 void gainRoot() {
        Log.d(TAG, "gainRoot start");
        Runtime runtime = Runtime.getRuntime();
        Process process = null;
        try {
            process = runtime.exec("su\n");
            process.getOutputStream().write("exit\n".getBytes());
            process.getOutputStream().write("exit\n".getBytes());
            process.waitFor();
        } catch (Exception e) {
            
        } finally {
            if (process != null) {
                process.destroy();
            }
        }
        Log.d(TAG, "gainRoot finish");
    }

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

    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()); 
            if (waitFor) {
                process.waitFor();
            }

            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) {
                    
                    Thread.sleep(100);
                    Log.d(TAG, "exec wait to get exitValue!");
                }
            }

        } catch (Exception e) {
            
            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();
    }
}

 
继续阅读
编程十万个怎么办