Skip to content

Commit 3334e56

Browse files
committed
添加执行同步任务
1 parent aece8c1 commit 3334e56

File tree

1 file changed

+49
-2
lines changed

1 file changed

+49
-2
lines changed

AndroidExecLibrary/src/main/java/com/excellence/exec/CommandTask.java

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,16 @@ public Builder commands(List<String> commands) {
9696
* @param commands
9797
* @return
9898
*/
99-
public Builder commands(String[] commands) {
99+
public Builder commands(String... commands) {
100100
return commands(Arrays.asList(commands));
101101
}
102102

103+
/**
104+
* 任务命令:字符串带空格转字符串数组命令
105+
*
106+
* @param command
107+
* @return
108+
*/
103109
public Builder commands(String command) {
104110
String[] commands = command.split(" ");
105111
return commands(commands);
@@ -145,7 +151,48 @@ public CommandTask build() {
145151
}
146152

147153
/**
148-
* 添加到任务队列中
154+
* 同步执行任务
155+
*
156+
* @return
157+
*/
158+
public static String exec(String command) {
159+
String[] commands = command.split(" ");
160+
return exec(commands);
161+
}
162+
163+
/**
164+
* 同步执行任务
165+
*
166+
* @return
167+
*/
168+
public static String exec(String... commands) {
169+
return exec(Arrays.asList(commands));
170+
}
171+
172+
/**
173+
* 同步执行任务
174+
*
175+
* @return
176+
*/
177+
public static String exec(List<String> commands) {
178+
StringBuilder result = new StringBuilder();
179+
try {
180+
Process process = new ProcessBuilder(commands).redirectErrorStream(true).start();
181+
182+
BufferedReader stdin = new BufferedReader(new InputStreamReader(process.getInputStream()));
183+
String line = null;
184+
while ((line = stdin.readLine()) != null) {
185+
result.append(line);
186+
}
187+
stdin.close();
188+
} catch (Exception e) {
189+
e.printStackTrace();
190+
}
191+
return result.toString();
192+
}
193+
194+
/**
195+
* 添加到任务队列中,异步执行
149196
*
150197
* @param listener
151198
*/

0 commit comments

Comments
 (0)