Android应用中执行shell脚本的工具类的示例
2015-05-17 22:28
399 查看
原文链接:http://blog.csdn.net/sowhat_ah/article/details/43228621
以下是工具类内容:
以下是工具类内容:
package com.example.test; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import android.util.Log; /** * 执行shell脚本工具类 * @author Mountain * */ public 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; } }
相关文章推荐
- Android执行shell脚本命令(备忘记录与实际应用)
- Android apk执行shell脚本 工具类
- Azkaban实战,Command类型单一job示例,任务中执行外部shell脚本,Command类型多job工作flow,HDFS操作任务,MapReduce任务,HIVE任务
- android启动时,如何执行shell脚本
- 1011.在线视频―shell脚本系列讲座(一)shell脚本与应用示例
- ubuntu Android环境的搭建及其shell执行脚本
- Android 执行shell脚本
- 用shell写一个简易计算器,可以实现加、减、乘、除运算,假如脚本名字为1.sh,执行示例:./1.
- Android开机执行shell脚本
- Android 7.0 init.rc 执行shell脚本 ---- 制作一初始化配置文件
- batT脚本如何自动执行 adb shell 以后的命令(android抓包)
- android应用执行需要root权限的shell命令
- android执行shell脚本
- 【Android 应用开发】Android 上实现非root的 Traceroute -- 非Root权限下移植可执行二进制文件 脚本文件
- Android 应用通过init.rc启动service去执行具有root权限sh脚本
- Android开机执行shell脚本
- android 在手机sd 卡中执行linux 命令 和shell 脚本
- Android开机执行shell脚本
- Java实现的执行python脚本工具类示例【使用jython.jar】
- android应用程序执行shell脚本命令