Skip to content

Commit 29a93f6

Browse files
committed
主线程中回调
1 parent 409ea23 commit 29a93f6

File tree

3 files changed

+109
-42
lines changed

3 files changed

+109
-42
lines changed

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

Lines changed: 56 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
package com.excellence.exec;
22

3+
import android.os.Handler;
4+
import android.os.Looper;
5+
import android.support.annotation.NonNull;
6+
37
import java.io.BufferedReader;
48
import java.io.InputStreamReader;
59
import java.util.LinkedList;
610
import java.util.List;
11+
import java.util.concurrent.Executor;
712
import java.util.concurrent.TimeUnit;
813

914
import io.reactivex.Observable;
@@ -30,6 +35,7 @@ public class Command {
3035
private final LinkedList<CommandTask> mTaskQueue;
3136
private int mParallelTaskCount = 0;
3237
private int mTimeOut = 0;
38+
private Executor mResponsePoster = null;
3339

3440
protected Command(CommanderOptions options) {
3541
mTaskQueue = new LinkedList<>();
@@ -41,6 +47,14 @@ protected Command(CommanderOptions options) {
4147
if (mTimeOut <= 0) {
4248
mTimeOut = DEFAULT_TIME_OUT;
4349
}
50+
51+
final Handler handler = new Handler(Looper.getMainLooper());
52+
mResponsePoster = new Executor() {
53+
@Override
54+
public void execute(@NonNull Runnable command) {
55+
handler.post(command);
56+
}
57+
};
4458
}
4559

4660
public CommandTask addTask(List<String> command, IListener listener) {
@@ -106,37 +120,57 @@ private CommandTask(List<String> command, final IListener listener) {
106120
mCommand = command;
107121
mIListener = new IListener() {
108122
@Override
109-
public void onPre(String command) {
110-
if (listener != null) {
111-
listener.onPre(command);
112-
}
123+
public void onPre(final String command) {
124+
mResponsePoster.execute(new Runnable() {
125+
@Override
126+
public void run() {
127+
if (listener != null) {
128+
listener.onPre(command);
129+
}
130+
}
131+
});
113132
}
114133

115134
@Override
116-
public void onProgress(String message) {
117-
if (listener != null) {
118-
listener.onProgress(message);
119-
}
135+
public void onProgress(final String message) {
136+
mResponsePoster.execute(new Runnable() {
137+
@Override
138+
public void run() {
139+
if (listener != null) {
140+
listener.onProgress(message);
141+
}
142+
}
143+
});
120144
}
121145

122146
@Override
123-
public void onError(Throwable t) {
124-
if (mStatus != STATUS_INTERRUPT) {
125-
if (listener != null) {
126-
listener.onError(t);
147+
public void onError(final Throwable t) {
148+
mResponsePoster.execute(new Runnable() {
149+
@Override
150+
public void run() {
151+
if (mStatus != STATUS_INTERRUPT) {
152+
if (listener != null) {
153+
listener.onError(t);
154+
}
155+
}
156+
mStatus = STATUS_FINISHED;
157+
remove(CommandTask.this);
127158
}
128-
}
129-
mStatus = STATUS_FINISHED;
130-
remove(CommandTask.this);
159+
});
131160
}
132161

133162
@Override
134-
public void onSuccess(String message) {
135-
mStatus = STATUS_FINISHED;
136-
if (listener != null) {
137-
listener.onSuccess(message);
138-
}
139-
remove(CommandTask.this);
163+
public void onSuccess(final String message) {
164+
mResponsePoster.execute(new Runnable() {
165+
@Override
166+
public void run() {
167+
mStatus = STATUS_FINISHED;
168+
if (listener != null) {
169+
listener.onSuccess(message);
170+
}
171+
remove(CommandTask.this);
172+
}
173+
});
140174
}
141175
};
142176
}
@@ -161,6 +195,7 @@ public void subscribe(ObservableEmitter<String> emitter) throws Exception {
161195
}
162196
mCmd = cmd.toString();
163197
mIListener.onPre(mCmd);
198+
164199
restartTimer();
165200
mProcess = new ProcessBuilder(mCommand).redirectErrorStream(true).start();
166201

sample/src/main/java/com/excellence/exec/sample/MainActivity.java

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import android.support.v7.app.AppCompatActivity;
44
import android.os.Bundle;
55
import android.util.Log;
6+
import android.view.View;
7+
import android.widget.Button;
8+
import android.widget.TextView;
69

710
import com.excellence.exec.Command.CommandTask;
811
import com.excellence.exec.Commander;
@@ -13,33 +16,50 @@ public class MainActivity extends AppCompatActivity {
1316

1417
private static final String TAG = MainActivity.class.getSimpleName();
1518

19+
private Button mButton = null;
20+
private TextView mTextView = null;
21+
1622
@Override
1723
protected void onCreate(Bundle savedInstanceState) {
1824
super.onCreate(savedInstanceState);
1925
setContentView(R.layout.activity_main);
2026

21-
Commander.init(new CommanderOptions.Builder().setTimeOut(1000).build());
22-
final CommandTask task = Commander.addTask("ls", new IListener() {
23-
@Override
24-
public void onPre(String command) {
25-
Log.i(TAG, "onPre: " + command);
26-
}
27+
mButton = (Button) findViewById(R.id.button);
28+
mTextView = (TextView) findViewById(R.id.text);
2729

30+
mButton.setOnClickListener(new View.OnClickListener() {
2831
@Override
29-
public void onProgress(String message) {
30-
Log.i(TAG, "onProgress: " + message);
31-
}
32+
public void onClick(View v) {
33+
Commander.init(new CommanderOptions.Builder().setTimeOut(1000).build());
34+
mTextView.setText("");
35+
final CommandTask task = Commander.addTask("ls", new IListener() {
36+
@Override
37+
public void onPre(String command) {
38+
Log.i(TAG, "onPre: " + command);
39+
mTextView.append(command + "\n");
40+
}
3241

33-
@Override
34-
public void onError(Throwable t) {
35-
t.printStackTrace();
36-
}
42+
@Override
43+
public void onProgress(String message) {
44+
Log.i(TAG, "onProgress: " + message);
45+
mTextView.append(message + "\n");
46+
}
3747

38-
@Override
39-
public void onSuccess(String message) {
40-
Log.i(TAG, "onSuccess: " + message);
48+
@Override
49+
public void onError(Throwable t) {
50+
t.printStackTrace();
51+
mTextView.setText("Error:" + t.getMessage());
52+
}
53+
54+
@Override
55+
public void onSuccess(String message) {
56+
Log.i(TAG, "onSuccess: " + message);
57+
mTextView.append(message + "\n");
58+
}
59+
});
60+
// task.discard();
4161
}
4262
});
43-
// task.discard();
63+
4464
}
4565
}

sample/src/main/res/layout/activity_main.xml

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,25 @@
77
android:layout_height="match_parent"
88
tools:context=".MainActivity">
99

10-
<TextView
10+
<Button
11+
android:id="@+id/button"
1112
android:layout_width="wrap_content"
1213
android:layout_height="wrap_content"
13-
android:text="Hello World!"
14-
app:layout_constraintBottom_toBottomOf="parent"
14+
android:text="测试命令:ls"
15+
android:textAllCaps="false"/>
16+
17+
<ScrollView
18+
android:layout_width="match_parent"
19+
android:layout_height="wrap_content"
1520
app:layout_constraintLeft_toLeftOf="parent"
1621
app:layout_constraintRight_toRightOf="parent"
17-
app:layout_constraintTop_toTopOf="parent"/>
22+
app:layout_constraintStart_toStartOf="parent"
23+
app:layout_constraintTop_toBottomOf="@+id/button">
24+
25+
<TextView
26+
android:id="@+id/text"
27+
android:layout_width="match_parent"
28+
android:layout_height="wrap_content"/>
29+
</ScrollView>
1830

1931
</android.support.constraint.ConstraintLayout>

0 commit comments

Comments
 (0)