Skip to content

Commit 93bbb9d

Browse files
committed
优化销毁进程
1 parent ea599bd commit 93bbb9d

File tree

2 files changed

+124
-44
lines changed

2 files changed

+124
-44
lines changed

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

Lines changed: 41 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -146,54 +146,50 @@ public void deploy(final IListener listener) {
146146
* 执行命令,由{@link Command#schedule()}控制
147147
*/
148148
void deploy() {
149-
try {
150-
// only wait task can deploy
151-
if (mStatus != STATUS_WAITING) {
152-
return;
153-
}
154-
mStatus = STATUS_RUNNING;
155-
mCommandTask = Observable.timer(mTimeDelay, mTimeUnit).subscribeOn(Schedulers.io()).subscribe(new Consumer<Long>() {
156-
@Override
157-
public void accept(Long aLong) throws Exception {
158-
if (mStatus == STATUS_INTERRUPT) {
159-
return;
160-
}
149+
// only wait task can deploy
150+
if (mStatus != STATUS_WAITING) {
151+
return;
152+
}
153+
mStatus = STATUS_RUNNING;
154+
mCommandTask = Observable.timer(mTimeDelay, mTimeUnit).subscribeOn(Schedulers.io()).subscribe(new Consumer<Long>() {
155+
@Override
156+
public void accept(Long aLong) throws Exception {
157+
if (mStatus == STATUS_INTERRUPT) {
158+
return;
159+
}
161160

162-
StringBuilder cmd = new StringBuilder();
163-
for (String item : mCommand) {
164-
cmd.append(item).append(" ");
165-
}
166-
mCmd = cmd.toString();
167-
mIListener.onPre(mCmd);
168-
169-
restartTimer();
170-
mProcess = new ProcessBuilder(mCommand).redirectErrorStream(true).start();
171-
172-
BufferedReader stdin = new BufferedReader(new InputStreamReader(mProcess.getInputStream()));
173-
StringBuilder result = new StringBuilder();
174-
String line = null;
175-
while ((line = stdin.readLine()) != null) {
176-
if (mStatus == STATUS_RUNNING) {
177-
restartTimer();
178-
mIListener.onProgress(line);
179-
result.append(line);
180-
}
181-
}
182-
stdin.close();
183-
resetTimer();
161+
StringBuilder cmd = new StringBuilder();
162+
for (String item : mCommand) {
163+
cmd.append(item).append(" ");
164+
}
165+
mCmd = cmd.toString();
166+
mIListener.onPre(mCmd);
167+
168+
restartTimer();
169+
mProcess = new ProcessBuilder(mCommand).redirectErrorStream(true).start();
170+
171+
BufferedReader stdin = new BufferedReader(new InputStreamReader(mProcess.getInputStream()));
172+
StringBuilder result = new StringBuilder();
173+
String line = null;
174+
while ((line = stdin.readLine()) != null) {
184175
if (mStatus == STATUS_RUNNING) {
185-
mIListener.onSuccess(result.toString());
176+
restartTimer();
177+
mIListener.onProgress(line);
178+
result.append(line);
186179
}
187180
}
188-
}, new Consumer<Throwable>() {
189-
@Override
190-
public void accept(Throwable throwable) throws Exception {
191-
mIListener.onError(throwable);
181+
stdin.close();
182+
resetTimer();
183+
if (mStatus == STATUS_RUNNING) {
184+
mIListener.onSuccess(result.toString());
192185
}
193-
});
194-
} catch (Exception e) {
195-
mIListener.onError(e);
196-
}
186+
}
187+
}, new Consumer<Throwable>() {
188+
@Override
189+
public void accept(Throwable throwable) throws Exception {
190+
mIListener.onError(throwable);
191+
}
192+
});
197193
}
198194

199195
private void resetTimer() {
@@ -218,9 +214,10 @@ protected boolean isRunning() {
218214
}
219215

220216
private void killProcess() {
217+
resetTimer();
221218
if (mProcess != null) {
222219
// close stream
223-
mProcess.destroy();
220+
ProcessUtils.processDestroy(mProcess);
224221
}
225222
}
226223

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.excellence.exec;
2+
3+
import android.support.annotation.NonNull;
4+
5+
import java.io.InputStream;
6+
import java.io.OutputStream;
7+
import java.util.regex.Matcher;
8+
import java.util.regex.Pattern;
9+
10+
/**
11+
* <pre>
12+
* author : VeiZhang
13+
* blog : http://tiimor.cn
14+
* time : 2018/9/3
15+
* desc : {@link Process} 进程工具
16+
* </pre>
17+
*/
18+
public class ProcessUtils {
19+
20+
public static final String REG_NUMBER = "[^0-9]";
21+
22+
public static void killProcess(@NonNull Process process) {
23+
int pid = getProcessId(process);
24+
if (pid <= 0) {
25+
return;
26+
}
27+
android.os.Process.killProcess(pid);
28+
process.destroy();
29+
}
30+
31+
public static int getProcessId(@NonNull Process process) {
32+
try {
33+
Pattern pattern = Pattern.compile(REG_NUMBER);
34+
Matcher matcher = pattern.matcher(process.toString());
35+
String pid = matcher.replaceAll("").trim();
36+
return Integer.parseInt(pid);
37+
} catch (Exception e) {
38+
39+
}
40+
return 0;
41+
}
42+
43+
public static void closeStream(@NonNull Process process) {
44+
try {
45+
InputStream in = process.getInputStream();
46+
if (in != null) {
47+
in.close();
48+
}
49+
} catch (Exception e) {
50+
51+
}
52+
53+
try {
54+
InputStream in = process.getErrorStream();
55+
if (in != null) {
56+
in.close();
57+
}
58+
} catch (Exception e) {
59+
60+
}
61+
62+
try {
63+
OutputStream out = process.getOutputStream();
64+
if (out != null) {
65+
out.close();
66+
}
67+
} catch (Exception e) {
68+
69+
}
70+
}
71+
72+
public static void processDestroy(@NonNull Process process) {
73+
try {
74+
if (process.exitValue() != 0) {
75+
closeStream(process);
76+
killProcess(process);
77+
}
78+
} catch (Exception e) {
79+
closeStream(process);
80+
killProcess(process);
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)