RxJava/RxAndroid's operator : just
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;
public class MainActivity extends AppCompatActivity {
private String TAG = "输出";
//观察者(或称之为订阅者)。
private DisposableObserver<String> mDisposableObserver = new DisposableObserver<String>() {
@Override
public void onNext(String string) {
Log.d(TAG,"onNext:" + string);
}
@Override
public void onComplete() {
Log.d(TAG, "onComplete");
}
@Override
public void onError(Throwable e) {
Log.e(TAG, e.toString(), e);
}
};
private CompositeDisposable mCompositeDisposable = new CompositeDisposable();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//包含被观察者的代码模块。
Disposable mDisposable = Observable.just("zhang", "phil", "book")
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(mDisposableObserver);
//把被观察者代放入到CompositeDisposable中,集中管理和维护。
mCompositeDisposable.add(mDisposable);
}
@Override
protected void onDestroy() {
super.onDestroy();
//应用退出,清空所有被观察者。
mCompositeDisposable.clear();
}
}