Android程序中如何执⾏shell脚本
在做应⽤时,经常需要执⾏shell脚本,以快速实现某些功能;
在Android应⽤程序中执⾏shell脚本可以省去⼀⼤堆繁琐的代码,还可以避免不必要的错误;
⽐如:拷贝⽂件夹时,可以执⾏shell命令中的 cp 命令达到⽬的;⽽在代码中实现拷贝⽂件夹时,不仅需要编写⼀⼤堆繁琐的代码,还容易陷⼊递归死循环的错误中;
⽐如:获取⽂件系统的读写权限,只需要执⾏shell脚本中⼀句 mount -o rw,remount / 就能轻松搞定;
⽐如:删除⽂件夹下某⼀个⽂件、或者某⼀类⽂件、或者全部⽂件,只需要执⾏shell脚本中的⼀句 rm -f *(利⽤*通配符进⾏匹配) 就能轻松搞定;
再⽐如:静默安装时,只需要执⾏shell脚本中⼀句 pm install -r 便可达到⽬的;
如果这些都⽤代码来实现,不仅代码量增加,还容易造成很多bug,吃⼒不讨好!
如果能在android应⽤中执⾏shell脚本来达到⽬的,可以省去⼀⼤堆代码,避免很多易犯的错误,简洁⾼效,何乐⽽不为呢?!
下⾯给出⼀个在Android应⽤中执⾏shell脚本的⼯具类的⽰例,供⼤家参考:
st;
2
3import java.io.BufferedReader;
4import java.io.DataOutputStream;
5import java.io.IOException;
6import java.io.InputStreamReader;
7
8import android.util.Log;
9
10/**
11 * 执⾏shell脚本⼯具类
12 * @author Mountain
13 *
14*/
15public class CommandExecution {
16
17public static final String TAG = "CommandExecution";
18
19public final static String COMMAND_SU = "su";
20public final static String COMMAND_SH = "sh";
21public final static String COMMAND_EXIT = "exit\n";
22public final static String COMMAND_LINE_END = "\n";
23
24/**
25 * Command执⾏结果
26 * @author Mountain
27 *
28*/
29public static class CommandResult {
30public int result = -1;
31public String errorMsg;
32public String successMsg;
33 }
34
35/**
36 * 执⾏命令—单条
37 * @param command
38 * @param isRoot
39 * @return
40*/
41public static CommandResult execCommand(String command, boolean isRoot) {
42 String[] commands = {command};
43return execCommand(commands, isRoot);
44 }
45
46/**
47 * 执⾏命令-多条
48 * @param commands
49 * @param isRoot
50 * @return
51*/
52public static CommandResult execCommand(String[] commands, boolean isRoot) {
53 CommandResult commandResult = new CommandResult();
54if (commands == null || commands.length == 0) return 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;
61try {
62 process = Runtime().exec(isRoot ? COMMAND_SU : COMMAND_SH);
63 os = new OutputStream());
64for (String command : commands) {
65if (command != null) {
66 os.Bytes());
67 os.writeBytes(COMMAND_LINE_END);
68 os.flush();
69 }
70 }
71 os.writeBytes(COMMAND_EXIT);
72 os.flush();
73 sult = process.waitFor();
74//获取错误信息
75 successMsg = new StringBuilder();
76 errorMsg = new StringBuilder();
77 successResult = new BufferedReader(new InputStream()));
78 errorResult = new BufferedReader(new ErrorStream()));
79 String s;
80while ((s = adLine()) != null) successMsg.append(s);
81while ((s = adLine()) != null) errorMsg.append(s);
82 commandResult.successMsg = String();
83 Msg = String();
84 Log.i(TAG, sult + " | " + commandResult.successMsg
85 + " | " + Msg);
86 } catch (IOException e) {
87 String errmsg = e.getMessage();
88if (errmsg != null) {
89 Log.e(TAG, errmsg);
90 } else {
91 e.printStackTrace();
92 }
93 } catch (Exception e) {
94 String errmsg = e.getMessage();
95if (errmsg != null) {
96 Log.e(TAG, errmsg);
97 } else {
98 e.printStackTrace();
99 }
100 } finally {
101try {
102if (os != null) os.close();
103if (successResult != null) successResult.close();
shell代码104if (errorResult != null) errorResult.close();
105 } catch (IOException e) {
106 String errmsg = e.getMessage();
107if (errmsg != null) {
108 Log.e(TAG, errmsg);
109 } else {
110 e.printStackTrace();
111 }
112 }
113if (process != null) process.destroy();
114 }
115return commandResult;
116 }
117
118 }
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论