RxJava/RxAndroid : doAfterNext
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
public class MainActivity extends AppCompatActivity {
private String TAG = "输出";
private Observer<Integer> mObserver = new Observer<Integer>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(Integer i) {
Log.d(TAG, "onNext:" + i);
}
@Override
public void onComplete() {
Log.d(TAG, "onComplete");
}
@Override
public void onError(Throwable e) {
Log.e(TAG, e.toString(), e);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Integer[] numbers = new Integer[]{0, 1, 2, 3, 4};
Observable.fromArray(numbers)
.takeLast(3)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doAfterNext(new Consumer<Integer>() {
@Override
public void accept(Integer integer) throws Exception {
Log.d(TAG, "accept:" + integer);
}
})
.subscribeWith(mObserver);
}
}
Output:
01-19 15:29:10.092 20375-20375/zhangphil.book D/输出: onNext:2
01-19 15:29:10.092 20375-20375/zhangphil.book D/输出: accept:2
01-19 15:29:10.092 20375-20375/zhangphil.book D/输出: onNext:3
01-19 15:29:10.092 20375-20375/zhangphil.book D/输出: accept:3
01-19 15:29:10.092 20375-20375/zhangphil.book D/输出: onNext:4
01-19 15:29:10.092 20375-20375/zhangphil.book D/输出: accept:4
01-19 15:29:10.092 20375-20375/zhangphil.book D/输出: onComplete