如何在代码中跑Linux指令

来源:互联网 发布:淘宝怎么搜索同城店铺 编辑:程序博客网 时间:2024/06/14 14:17

在手机端的开发过程中,有时候我们可能要在代码中运行Linux指令,从而去更改系统的某些配置,今天我们就来学习一下如何在代码中去运行Linux指令,废话不多说,我们直接看实现的工具类,以及调用的方法。

调用的工具类 CommandExecution.javapublic class CommandExecution {    public static final String TAG = "CommandExecution";    public final static String COMMAND_SU = "su";    public final static String COMMAND_SH = "sh";    public final static String COMMAND_EXIT = "exit\n";    public final static String COMMAND_LINE_END = "\n";    /**     * Command执行结果     *     * @author Mountain     */    public static class CommandResult {        public int result = -1;        public String errorMsg;        public String successMsg;    }    /**     * 执行命令—单条     *     * @param command     * @param isRoot     * @return     */    public static CommandResult execCommand(String command, boolean isRoot) {        String[] commands = {command};        return execCommand(commands, isRoot);    }    /**     * 执行命令-多条     *     * @param commands     * @param isRoot     * @return     */    public static CommandResult execCommand(String[] commands, boolean isRoot) {        CommandResult commandResult = new CommandResult();        if (commands == null || commands.length == 0) return commandResult;        Process process = null;        DataOutputStream os = null;        BufferedReader successResult = null;        BufferedReader errorResult = null;        StringBuilder successMsg = null;        StringBuilder errorMsg = null;        try {            process = Runtime.getRuntime().exec(isRoot ? COMMAND_SU : COMMAND_SH);            os = new DataOutputStream(process.getOutputStream());            for (String command : commands) {                if (command != null) {                    os.write(command.getBytes());                    os.writeBytes(COMMAND_LINE_END);                    os.flush();                }            }            os.writeBytes(COMMAND_EXIT);            os.flush();            commandResult.result = process.waitFor();            //获取错误信息            successMsg = new StringBuilder();            errorMsg = new StringBuilder();            successResult = new BufferedReader(new InputStreamReader(process.getInputStream()));            errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream()));            String s;            while ((s = successResult.readLine()) != null) successMsg.append(s);            while ((s = errorResult.readLine()) != null) errorMsg.append(s);            commandResult.successMsg = successMsg.toString();            commandResult.errorMsg = errorMsg.toString();            Log.i(TAG, commandResult.result + " | " + commandResult.successMsg                    + " | " + commandResult.errorMsg);        } catch (IOException e) {            String errmsg = e.getMessage();            if (errmsg != null) {                Log.e(TAG, errmsg);            } else {                e.printStackTrace();            }        } catch (Exception e) {            String errmsg = e.getMessage();            if (errmsg != null) {                Log.e(TAG, errmsg);            } else {                e.printStackTrace();            }        } finally {            try {                if (os != null) os.close();                if (successResult != null) successResult.close();                if (errorResult != null) errorResult.close();            } catch (IOException e) {                String errmsg = e.getMessage();                if (errmsg != null) {                    Log.e(TAG, errmsg);                } else {                    e.printStackTrace();                }            }            if (process != null) process.destroy();        }        return commandResult;    }}
工具类的简单调用以及运行指令的方法: String[] commands = {"mount -o remount /dev/block/by-name/system /system",                        "cd system", "sed -i 's/^qemu.*/qemu.hw.mainkeys=1/'                 build.prop", "cd ..", "reboot"};CommandExecution.CommandResult commandResult = CommandExecution.execCommand(commands, true); System.out.println("commandResult.errorMsg:" + commandResult.errorMsg + "/" + "commandResult.result:" + commandResult.result + "/"                        + "commandResult.successMsg:" + commandResult.successMsg                );
0 0