Skip to content

Commit 90f25fc

Browse files
committed
新增延时任务接口
1 parent 2367e95 commit 90f25fc

File tree

3 files changed

+41
-39
lines changed

3 files changed

+41
-39
lines changed

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

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
import java.util.concurrent.TimeUnit;
1111

1212
import io.reactivex.Observable;
13-
import io.reactivex.ObservableEmitter;
14-
import io.reactivex.ObservableOnSubscribe;
1513
import io.reactivex.disposables.Disposable;
1614
import io.reactivex.functions.Consumer;
1715
import io.reactivex.schedulers.Schedulers;
@@ -43,7 +41,8 @@ public class CommandTask {
4341
private Process mProcess = null;
4442
private int mStatus = STATUS_WAITING;
4543
private CommandTaskListener mIListener = null;
46-
private Disposable mTimer = null;
44+
private Disposable mCommandTask = null;
45+
private Disposable mTimerTask = null;
4746
private String mCmd = null;
4847

4948
private CommandTask(Builder builder) {
@@ -155,9 +154,9 @@ void deploy() {
155154
return;
156155
}
157156
mStatus = STATUS_RUNNING;
158-
Observable.create(new ObservableOnSubscribe<String>() {
157+
mCommandTask = Observable.timer(mTimeDelay, mTimeUnit).subscribeOn(Schedulers.io()).subscribe(new Consumer<Long>() {
159158
@Override
160-
public void subscribe(ObservableEmitter<String> emitter) throws Exception {
159+
public void accept(Long aLong) throws Exception {
161160
if (mStatus == STATUS_INTERRUPT) {
162161
return;
163162
}
@@ -188,11 +187,6 @@ public void subscribe(ObservableEmitter<String> emitter) throws Exception {
188187
mIListener.onSuccess(result.toString());
189188
}
190189
}
191-
}).subscribeOn(Schedulers.io()).subscribe(new Consumer<String>() {
192-
@Override
193-
public void accept(String s) throws Exception {
194-
195-
}
196190
}, new Consumer<Throwable>() {
197191
@Override
198192
public void accept(Throwable throwable) throws Exception {
@@ -205,14 +199,14 @@ public void accept(Throwable throwable) throws Exception {
205199
}
206200

207201
private void resetTimer() {
208-
if (mTimer != null && !mTimer.isDisposed()) {
209-
mTimer.dispose();
202+
if (mTimerTask != null && !mTimerTask.isDisposed()) {
203+
mTimerTask.dispose();
210204
}
211205
}
212206

213207
private void restartTimer() {
214208
resetTimer();
215-
mTimer = Observable.timer(mTimeOut, mTimeUnit).subscribe(new Consumer<Long>() {
209+
mTimerTask = Observable.timer(mTimeOut, mTimeUnit).subscribe(new Consumer<Long>() {
216210
@Override
217211
public void accept(Long aLong) throws Exception {
218212
mIListener.onError(new Throwable("Time out : " + mCmd));
@@ -230,6 +224,9 @@ private void killProcess() {
230224
// close stream
231225
mProcess.destroy();
232226
}
227+
if (mCommandTask != null && !mCommandTask.isDisposed()) {
228+
mCommandTask.dispose();
229+
}
233230
}
234231

235232
protected void cancel() {

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ Commander.init();
1818
Commander.init(new CommanderOptions.Builder().setTimeOut(1000).build())
1919
2020
// 创建执行命令
21+
推荐方式:
22+
new CommandTask.Builder().command("ls").build().deploy(Listener);
23+
24+
丢弃方式:
2125
Commander.addTask("ls", new IListener() {
2226
@Override
2327
public void onPre(String command) {

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

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import com.excellence.exec.CommanderOptions;
1313
import com.excellence.exec.IListener;
1414

15-
public class MainActivity extends AppCompatActivity {
15+
public class MainActivity extends AppCompatActivity implements IListener {
1616

1717
private static final String TAG = MainActivity.class.getSimpleName();
1818

@@ -32,34 +32,35 @@ protected void onCreate(Bundle savedInstanceState) {
3232
public void onClick(View v) {
3333
Commander.init(new CommanderOptions.Builder().setTimeOut(1000).build());
3434
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-
}
41-
42-
@Override
43-
public void onProgress(String message) {
44-
Log.i(TAG, "onProgress: " + message);
45-
mTextView.append(message + "\n");
46-
}
47-
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-
});
35+
new CommandTask.Builder().command("ls").build().deploy(MainActivity.this);
36+
// final CommandTask task = Commander.addTask("ls", MainActivity.this);
6037
// task.discard();
6138
}
6239
});
6340

6441
}
42+
43+
@Override
44+
public void onPre(String command) {
45+
Log.i(TAG, "onPre: " + command);
46+
mTextView.append(command + "\n");
47+
}
48+
49+
@Override
50+
public void onProgress(String message) {
51+
//Log.i(TAG, "onProgress: " + message);
52+
mTextView.append(message + "\n");
53+
}
54+
55+
@Override
56+
public void onError(Throwable t) {
57+
t.printStackTrace();
58+
mTextView.setText("Error:" + t.getMessage());
59+
}
60+
61+
@Override
62+
public void onSuccess(String message) {
63+
Log.i(TAG, "onSuccess: " + message);
64+
mTextView.append(message + "\n");
65+
}
6566
}

0 commit comments

Comments
 (0)