Android响应式编程开发RxAndroid(5):RxJava 2.0的Consumer
RxJava 2.0引入的Consumer相当于1.0时候的Action。如果只是想简洁的获得处理结果,则可以使用RxJava 2.0引入到的Consumer。例如:
package zhangphil.app;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import java.util.Arrays;
import java.util.List;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;
public class MainActivity extends Activity {
//假设一个数据源
private String TAG = "zhangphil测试";
private Observable mObservable;
private DisposableObserver<String> mObserver = new DisposableObserver<String>() {
@Override
public void onNext(String s) {
Log.d(TAG, "onNext - Start");
Log.d(TAG, String.valueOf(s));
Log.d(TAG, "onNext - End");
}
@Override
public void onComplete() {
Log.d(TAG, "onComplete");
}
@Override
public void onError(Throwable e) {
Log.e(TAG, e.toString(), e);
}
};
//如果只是接受处理结果,那么可以用Consumer(消费)
private Consumer mConsumer = new Consumer<String>() {
@Override
public void accept(String s) throws Exception {
Log.d(TAG, "accept - Start");
Log.d(TAG, String.valueOf(s));
Log.d(TAG, "accept - End");
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] source = {"zhang", "phil", "@", "csdn"};
List<String> lists = Arrays.asList(source);
mObservable = Observable
.just(lists)
.subscribeOn(Schedulers.io())//执行任务的线程
.observeOn(AndroidSchedulers.mainThread()) //回调发生的线程
.flatMap(mFunction);
//建立订阅关系
mObservable.subscribe(mConsumer);
mObservable.subscribe(mObserver);
}
// 设置映射函数,实现简单功能:小写转化为大写
private Function<List<String>, Observable<String>> mFunction = new Function<List<String>, Observable<String>>() {
@Override
public Observable<String> apply(List<String> lists) {
String[] strs = new String[lists.size()];
for (int i = 0; i < lists.size(); i++) {
strs[i] = lists.get(i).toUpperCase();
}
return Observable.fromArray(strs);
}
};
}
代码运行结果如图:
03-02 13:53:55.046 31633-31633/zhangphil.app D/zhangphil测试: accept - Start
03-02 13:53:55.046 31633-31633/zhangphil.app D/zhangphil测试: ZHANG
03-02 13:53:55.046 31633-31633/zhangphil.app D/zhangphil测试: accept - End
03-02 13:53:55.046 31633-31633/zhangphil.app D/zhangphil测试: accept - Start
03-02 13:53:55.046 31633-31633/zhangphil.app D/zhangphil测试: PHIL
03-02 13:53:55.046 31633-31633/zhangphil.app D/zhangphil测试: accept - End
03-02 13:53:55.046 31633-31633/zhangphil.app D/zhangphil测试: accept - Start
03-02 13:53:55.046 31633-31633/zhangphil.app D/zhangphil测试: @
03-02 13:53:55.046 31633-31633/zhangphil.app D/zhangphil测试: accept - End
03-02 13:53:55.046 31633-31633/zhangphil.app D/zhangphil测试: accept - Start
03-02 13:53:55.046 31633-31633/zhangphil.app D/zhangphil测试: CSDN
03-02 13:53:55.046 31633-31633/zhangphil.app D/zhangphil测试: accept - End
03-02 13:53:55.046 31633-31633/zhangphil.app D/zhangphil测试: onNext - Start
03-02 13:53:55.046 31633-31633/zhangphil.app D/zhangphil测试: ZHANG
03-02 13:53:55.046 31633-31633/zhangphil.app D/zhangphil测试: onNext - End
03-02 13:53:55.046 31633-31633/zhangphil.app D/zhangphil测试: onNext - Start
03-02 13:53:55.046 31633-31633/zhangphil.app D/zhangphil测试: PHIL
03-02 13:53:55.046 31633-31633/zhangphil.app D/zhangphil测试: onNext - End
03-02 13:53:55.046 31633-31633/zhangphil.app D/zhangphil测试: onNext - Start
03-02 13:53:55.046 31633-31633/zhangphil.app D/zhangphil测试: @
03-02 13:53:55.046 31633-31633/zhangphil.app D/zhangphil测试: onNext - End
03-02 13:53:55.046 31633-31633/zhangphil.app D/zhangphil测试: onNext - Start
03-02 13:53:55.046 31633-31633/zhangphil.app D/zhangphil测试: CSDN
03-02 13:53:55.046 31633-31633/zhangphil.app D/zhangphil测试: onNext - End
03-02 13:53:55.046 31633-31633/zhangphil.app D/zhangphil测试: onComplete
附录:
1,《Android响应式编程RxAndroid(1):简单例程》链接地址:http://blog.csdn.net/zhangphil/article/details/58593782
2,《Android响应式编程开发RxAndroid(2):简单例程的Android改造 》链接地址:http://blog.csdn.net/zhangphil/article/details/58597372
3,《Android响应式编程开发RxAndroid(3):map》链接地址:http://blog.csdn.net/zhangphil/article/details/59130641
4,《Android响应式编程开发RxAndroid(4):flatMap》链接地址:http://blog.csdn.net/zhangphil/article/details/59483465