原文链接: https://blog.csdn.net/sowhat_Ah/article/details/43228621

在做Android应用时,经常需要执行shell脚本,以快速实现某些功能;

在Android应用程序中执行shell脚本可以省去一大堆繁琐的代码,还可以避免不必要的错误;

比如:拷贝文件夹时,可以执行shell命令中的 cp 命令达到目的;而在代码中实现拷贝文件夹时,不仅需要编写一大堆繁琐的代码,还容易陷入递归死循环的错误中;

比如:获取文件系统的读写权限,只需要执行shell脚本中一句 mount -o rw,remount / 就能轻松搞定;

比如:删除文件夹下某一个文件、或者某一类文件、或者全部文件,只需要执行shell脚本中的一句 rm -f (利用通配符进行匹配) 就能轻松搞定;

再比如:静默安装时,只需要执行shell脚本中一句 pm install -r 便可达到目的;
如果这些都用代码来实现,不仅代码量增加,还容易造成很多bug,吃力不讨好!

如果能在android应用中执行shell脚本来达到目的,可以省去一大堆代码,避免很多易犯的错误,简洁高效,何乐而不为呢?!

下面给出一个在Android应用中执行shell脚本的工具类的示例,供大家参考:

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

更多相关文章

  1. Android程序有很多Activity,在B中如何关闭整个Android应用程序
  2. android带动画的圆形进度条
  3. 秒懂Android注解处理器(Android(安卓)Annotation Processor)
  4. android studio module上传到jcenter
  5. 成品app直播源码,Android自屏幕底部滑出更多面板的实现
  6. android json访问php webservice
  7. android登录模块之静态登录
  8. Android(安卓)实现歌词同步
  9. Android(安卓)MVP 模式:简单易懂的介绍方式

随机推荐

  1. Android 简单笔记
  2. 第一次执行Cordova build android失败原
  3. Android(安卓)PMEM驱动研究 在应用程序中
  4. Android NDK so crash,定位目标代码使用
  5. Android和java创建xml文件和解析xml文件
  6. android获取屏幕长宽,status bar高度,设置
  7. Android Service学习之IntentService 深
  8. [置顶] Android okHttp上传单张或多张照
  9. Android Bigmap的操作(绘制,裁剪)
  10. Android EXIF