首页 > 代码库 > Android平台 Runtime.getRuntime().exec

Android平台 Runtime.getRuntime().exec

import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;public class RootCmd {    private static boolean mHaveRoot = false;    public static boolean haveRoot() {        if (!mHaveRoot) {            int ret = execRootCmdSilent("echo test");            if (ret != 1) {                System.out.println("have root!");                mHaveRoot = true;            } else {                System.out.println("not root!");            }        } else {            System.out.println("mHaveRoot = true, have root!");        }        return mHaveRoot;    }    @SuppressWarnings("deprecation")    public static String execRootCmd(String cmd) {        String result = "";        DataOutputStream dos = null;        DataInputStream dis = null;        try {            Process p = Runtime.getRuntime().exec("su");            dos = new DataOutputStream(p.getOutputStream());            dis = new DataInputStream(p.getInputStream());            System.out.println(cmd);            dos.writeBytes(cmd + "\n");            dos.flush();            dos.writeBytes("exit\n");            dos.flush();            String line = null;            while ((line = dis.readLine()) != null) {                result += line;            }            p.waitFor();        } catch (Exception e) {            e.printStackTrace();        } finally {            if (dos != null) {                try {                    dos.close();                } catch (IOException e) {                    e.printStackTrace();                }            }            if (dis != null) {                try {                    dis.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }        return result;    }    public static int execRootCmdSilent(String cmd) {        int result = -1;        DataOutputStream dos = null;        try {            Process p = Runtime.getRuntime().exec("su");            dos = new DataOutputStream(p.getOutputStream());            System.out.println(cmd);            dos.writeBytes(cmd + "\n");            dos.flush();            dos.writeBytes("exit\n");            dos.flush();            p.waitFor();            result = p.exitValue();        } catch (Exception e) {            e.printStackTrace();        } finally {            if (dos != null) {                try {                    dos.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }        return result;    }}