android执行shell脚本

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

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

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

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

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

再比如:静默安装时,只需要执行shell脚本中一句 pm install -r 便可达到目的;

如果这些都用代码来实现,不仅代码量增加,还容易造成很多bug,吃力不讨好!


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


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

[java]  view plain  copy
  1. package com.example.test;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.DataOutputStream;  
  5. import java.io.IOException;  
  6. import java.io.InputStreamReader;  
  7.   
  8. import android.util.Log;  
  9.   
  10. /** 
  11.  * 执行shell脚本工具类 
  12.  * @author Mountain 
  13.  * 
  14.  */  
  15. public class CommandExecution {  
  16.   
  17.     public static final String TAG = "CommandExecution";  
  18.       
  19.     public final static String COMMAND_SU       = "su";  
  20.     public final static String COMMAND_SH       = "sh";  
  21.     public final static String COMMAND_EXIT     = "exit\n";  
  22.     public final static String COMMAND_LINE_END = "\n";  
  23.   
  24.     /** 
  25.      * Command执行结果 
  26.      * @author Mountain 
  27.      * 
  28.      */  
  29.     public static class CommandResult {  
  30.         public int result = -1;  
  31.         public String errorMsg;  
  32.         public String successMsg;  
  33.     }  
  34.   
  35.     /** 
  36.      * 执行命令—单条 
  37.      * @param command 
  38.      * @param isRoot 
  39.      * @return 
  40.      */  
  41.     public static CommandResult execCommand(String command, boolean isRoot) {  
  42.         String[] commands = {command};  
  43.         return execCommand(commands, isRoot);  
  44.     }  
  45.   
  46.     /** 
  47.      * 执行命令-多条 
  48.      * @param commands 
  49.      * @param isRoot 
  50.      * @return 
  51.      */  
  52.     public static CommandResult execCommand(String[] commands, boolean isRoot) {  
  53.         CommandResult commandResult = new CommandResult();  
  54.         if (commands == null || commands.length == 0return commandResult;  
  55.         Process process = null;  
  56.         DataOutputStream os = null;  
  57.         BufferedReader successResult = null;  
  58.         BufferedReader errorResult = null;  
  59.         StringBuilder successMsg = null;  
  60.         StringBuilder errorMsg = null;  
  61.         try {  
  62.             process = Runtime.getRuntime().exec(isRoot ? COMMAND_SU : COMMAND_SH);  
  63.             os = new DataOutputStream(process.getOutputStream());  
  64.             for (String command : commands) {  
  65.                 if (command != null) {  
  66.                     os.write(command.getBytes());  
  67.                     os.writeBytes(COMMAND_LINE_END);  
  68.                     os.flush();  
  69.                 }  
  70.             }  
  71.             os.writeBytes(COMMAND_EXIT);  
  72.             os.flush();  
  73.             commandResult.result = process.waitFor();  
  74.             //获取错误信息  
  75.             successMsg = new StringBuilder();  
  76.             errorMsg = new StringBuilder();  
  77.             successResult = new BufferedReader(new InputStreamReader(process.getInputStream()));  
  78.             errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream()));  
  79.             String s;  
  80.             while ((s = successResult.readLine()) != null) successMsg.append(s);  
  81.             while ((s = errorResult.readLine()) != null) errorMsg.append(s);  
  82.             commandResult.successMsg = successMsg.toString();  
  83.             commandResult.errorMsg = errorMsg.toString();  
  84.             Log.i(TAG, commandResult.result + " | " + commandResult.successMsg  
  85.                     + " | " + commandResult.errorMsg);  
  86.         } catch (IOException e) {  
  87.             String errmsg = e.getMessage();  
  88.             if (errmsg != null) {  
  89.                 Log.e(TAG, errmsg);  
  90.             } else {  
  91.                 e.printStackTrace();  
  92.             }  
  93.         } catch (Exception e) {  
  94.             String errmsg = e.getMessage();  
  95.             if (errmsg != null) {  
  96.                 Log.e(TAG, errmsg);  
  97.             } else {  
  98.                 e.printStackTrace();  
  99.             }  
  100.         } finally {  
  101.             try {  
  102.                 if (os != null) os.close();  
  103.                 if (successResult != null) successResult.close();  
  104.                 if (errorResult != null) errorResult.close();  
  105.             } catch (IOException e) {  
  106.                 String errmsg = e.getMessage();  
  107.                 if (errmsg != null) {  
  108.                     Log.e(TAG, errmsg);  
  109.                 } else {  
  110.                     e.printStackTrace();  
  111.                 }  
  112.             }  
  113.             if (process != null) process.destroy();  
  114.         }  
  115.         return commandResult;  
  116.     }  
  117.       
  118. }  

猜你喜欢

转载自blog.csdn.net/sifanlook/article/details/79976044