Android执行shell命令封装


Android可直接调用Runtime执行shell命令来实现一些功能,在此进行了一个封装。

将需要执行的命令添加到一个数组,并判断是否已root,还有一个回调接口,执行完后把执行结果封装到一个实体类。还有几个重载的执行方法,主要是接收单个字符串的命令,还有自动判断root的。

    public static void execCommand(String[] commands, boolean isRoot,
            ShellCommandListener listener) throws IOException,
            InterruptedException, TimeoutException {

        int exitCode = -1;
        CommandResult result = null;
        if (commands == null || commands.length == 0) {
            result = new CommandResult(exitCode, null, null);
            listener.onCommandFinished(result);
        }

        Process process = null;
        BufferedReader successReader = null;
        BufferedReader errorReader = null;
        StringBuilder successMsg = null;
        StringBuilder errorMsg = null;

        DataOutputStream os = null;
        process = Runtime.getRuntime().exec(isRoot ? "su" : "sh");
        os = new DataOutputStream(process.getOutputStream());
        for (String command : commands) {
            if (command == null) {
                continue;
            }

            // donnot use os.writeBytes(commmand), avoid chinese charset
            // error
            os.write(command.getBytes());
            os.writeBytes("\n");
            os.flush();
        }
        os.writeBytes("exit\n");
        os.flush();

        exitCode = process.waitFor();
        successMsg = new StringBuilder();
        errorMsg = new StringBuilder();

        successReader = new BufferedReader(new InputStreamReader(
                process.getInputStream()));
        errorReader = new BufferedReader(new InputStreamReader(
                process.getErrorStream()));
        String s = null;
        while ((s = successReader.readLine()) != null) {
            successMsg.append(s + "\n");
        }
        while ((s = errorReader.readLine()) != null) {
            errorMsg.append(s + "\n");
        }

        if (exitCode == -257) {
            throw new TimeoutException();
        }

        try {
            if (os != null) {
                os.close();
            }
            if (successReader != null) {
                successReader.close();
            }
            if (errorReader != null) {
                errorReader.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (process != null) {
            process.destroy();
        }
        result = new CommandResult(exitCode, successMsg == null ? null
                : successMsg.toString(), errorMsg == null ? null
                : errorMsg.toString());

        listener.onCommandFinished(result);
    }

    /**
     * result of command,
     */
    public static class CommandResult {

        /** result of command **/
        public int exitCode;
        /** success message of command result **/
        public String successMsg;
        /** error message of command result **/
        public String errorMsg;

        public CommandResult(int result) {
            this.exitCode = result;
        }

        public CommandResult(int result, String successMsg, String errorMsg) {
            this.exitCode = result;
            this.successMsg = successMsg;
            this.errorMsg = errorMsg;
        }

        @Override
        public String toString() {
            return "exitCode=" + exitCode + "; successMsg=" + successMsg
                    + "; errorMsg=" + errorMsg;
        }
    }

    public interface ShellCommandListener {
        public void onCommandFinished(CommandResult result);
    }


  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值