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;
    }
}

Android平台 Runtime.getRuntime().exec,布布扣,bubuko.com

时间: 2024-08-04 14:09:21

Android平台 Runtime.getRuntime().exec的相关文章

Android: 通过Runtime.getRuntime().exec调用底层Linux下的程序或脚本

Android Runtime使得直接调用底层Linux下的可执行程序或脚本成为可能 比如Linux下写个测试工具,直接编译后apk中通过Runtime来调用 或者写个脚本,apk中直接调用,省去中间层或者JNI 这个至少效率应该比较高吧 代码: [java] view plaincopy 1 public class test extends Activity { 2 TextView text; 3 4 /** Called when the activity is first create

Android ProcessBuilder与Runtime.getRuntime().exec分别创建进程的区别

在Android中想要进行Ping,在不Root机器的情况下似乎还只能进行底层命调用才能实现. 因为在Java中要进行ICMP包发送需要Root权限. 于是只能通过创建进程来解决了,创建进程在Java中有两种方式,分别为: 1. 调用ProcessBuilder的构造函数后执行start() 2. 用Runtime.getRuntime().exec()方法执行 经过使用后发现两者有区别但是也并不是很大,两个例子说明: 1.调用ProcessBuilder的构造函数后执行start(): Pro

Android Runtime.getRuntime().exec

try { // Executes the command. Process process = Runtime.getRuntime().exec(cmd); // NOTE: You can write to stdin of the command using // process.getOutputStream(). BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStrea

通过Runtime.getRuntime().exec调用底层Linux下的程序或脚本

Android Runtime使得直接调用底层Linux下的可执行程序或脚本成为可能 比如Linux下写个测试工具,直接编译后apk中通过Runtime来调用 或者写个脚本,apk中直接调用,省去中间层或者JNI 这个至少效率应该比较高吧 代码: [java] view plaincopy public class test extends Activity { TextView text; /** Called when the activity is first created. */ @O

Runtime.getRuntime().exec中命令含有括号问题

在写批量执行bat工具的时候,想起了之前写的定时小工具里面的执行方法. 使用Runtime.getRuntime().exec方法. Runtime.getRuntime().exec("cmd /c start c:/test.bat") 这样就可以像dos窗口直接执行命令行一样. getRuntime返回Runtime的实例化对象,exec方法是返回Process对象. 查看Process类可以发现: The ProcessBuilder.start() and Runtime.e

Runtime.getRuntime.exec()执行linux脚本导致程序卡死有关问题

Runtime.getRuntime.exec()执行linux脚本导致程序卡死问题问题: 在Java程序中,通过Runtime.getRuntime().exec()执行一个Linux脚本导致程序被挂住,而在终端上直接执行这个脚本则没有任何问题.原因: 先来看Java代码: public final static void process1(String[] cmdarray) {        Process p = null;        BufferedReader br = null

Runtime.getRuntime().exec()调用外部程序

场景:linux下,在web工程里调用一个C++程序,实现代码如下: StringBuffer cmd = new StringBuffer();cmd.append("nohup "); ……System.out.println("执行程序命令:"+cmd.toString());String[] cmds = { "/bin/sh", "-c", cmd.toString()};Runtime.getRuntime().e

Java Runtime.getRuntime().exec 执行带空格命令解决办法

String command = OpenOffice_HOME + "program\\soffice -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\" -nofirststartwizard "; command = "cmd /c start "+command.replaceAll(" ","\" \""); P

Runtime.getRuntime().exec方法

Runtime.getRuntime().exec共有四个重载方法: public Process exec(String command) 在单独的进程中执行指定的字符串命令. public Process exec(String [] cmdArray) 在单独的进程中执行指定命令和变量 public Process exec(String command, String [] envp) 在指定环境的独立进程中执行指定命令和变量 public Process exec(String []