Skip to content

Commit b647505

Browse files
committed
优化进程中断控制
1 parent 93bbb9d commit b647505

File tree

2 files changed

+7
-15
lines changed

2 files changed

+7
-15
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public void accept(Long aLong) throws Exception {
171171
BufferedReader stdin = new BufferedReader(new InputStreamReader(mProcess.getInputStream()));
172172
StringBuilder result = new StringBuilder();
173173
String line = null;
174-
while ((line = stdin.readLine()) != null) {
174+
while (mStatus == STATUS_RUNNING && (line = stdin.readLine()) != null) {
175175
if (mStatus == STATUS_RUNNING) {
176176
restartTimer();
177177
mIListener.onProgress(line);
@@ -217,7 +217,7 @@ private void killProcess() {
217217
resetTimer();
218218
if (mProcess != null) {
219219
// close stream
220-
ProcessUtils.processDestroy(mProcess);
220+
ProcessUtils.destroyProcess(mProcess);
221221
}
222222
}
223223

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

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@ public class ProcessUtils {
2121

2222
public static void killProcess(@NonNull Process process) {
2323
int pid = getProcessId(process);
24-
if (pid <= 0) {
25-
return;
24+
if (pid > 0) {
25+
android.os.Process.killProcess(pid);
2626
}
27-
android.os.Process.killProcess(pid);
2827
process.destroy();
2928
}
3029

@@ -69,15 +68,8 @@ public static void closeStream(@NonNull Process process) {
6968
}
7069
}
7170

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-
}
71+
public static void destroyProcess(@NonNull Process process) {
72+
closeStream(process);
73+
killProcess(process);
8274
}
8375
}

0 commit comments

Comments
 (0)