RxJava的说明
建议,先查看一下官方网站,看一下官方说明:
RxJava:
https://github.com/ReactiveX/RxJava
RxAndroid:
https://github.com/ReactiveX/RxAndroid
RxJava的官方说明:
RxJava is a Java VM implementation of Reactive Extensions: a library for composing asynchronous and event-based programs by using observable sequences.
RxJava是一个基于事件流的链式调用,异步处理耗时操作的响应式扩展的库.
我们可以对比AsyncTask类,主要是异步处理一些耗时操作,然后在主UI线程来更新UI界面.
RxAndroid: Reactive Extensions for Android的官方说明:
This module adds the minimum classes to RxJava that make writing reactive components in Android applications easy and hassle-free. More specifically, it provides a Scheduler that schedules on the main thread or any given Looper.
一个简单的Demo
(1)在build.gradle文件中加入依赖
dependencies {
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
implementation 'io.reactivex.rxjava2:rxjava:2.2.8'
}
(2) 实现一个后台处理耗时操作,然后在主UI线程更新UI界面的功能
我们在主UI界面显示一个button,当我们点击button,然后模拟在后台从磁盘缓存中获取数据的耗时操作,操作完成后,再将读取的数据是主UI界面的一个textview控件上显示.
public class MainActivity extends AppCompatActivity {
private final static String TAG = "RxJavaDemo";
private TextView textView;
private Button button;
// 该变量用于模拟磁盘缓存中的数据
String diskCache = "从磁盘缓存中获取数据为:RxJavaDemo--Test";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
textView = (TextView) findViewById(R.id.textView);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.i(TAG,"button--onClick");
onDoRxJavaDemo();
}
});
}
private void onDoRxJavaDemo() {
Observable observable = Observable.create(new ObservableOnSubscribe<String>() {
@Override
public void subscribe(ObservableEmitter<String> emitter) throws Exception {
Log.d(TAG, "observable的工作线程为:" + Thread.currentThread().getName());
//模拟一个60秒耗时操作
Thread.sleep(1000*60);
emitter.onNext(diskCache);
emitter.onComplete();
}
});
Observer<String> observer = new Observer<String>() {
@Override
public void onSubscribe(Disposable d) {
Log.d(TAG, "observer---subscribe");
Log.d(TAG, " observer的工作线程为:" + Thread.currentThread().getName());
}
@Override
public void onNext(String value) {
Log.d(TAG, "observer---onNext--value:"+ value);
//更新UI界面
textView.setText(value);
}
@Override
public void onError(Throwable e) {
Log.d(TAG, "observer---onError");
}
@Override
public void onComplete() {
Log.d(TAG, "observer---onComplete");
}
};
observable.subscribeOn(Schedulers.io())
//.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(observer);
}
}
输出日志为:
05-08 20:51:04.267 10798-10798/com.readygo.rxjavademo I/RxJavaDemo: button--onClick
05-08 20:51:04.334 10798-10798/com.readygo.rxjavademo D/RxJavaDemo: observer---subscribe observer的工作线程为:main
05-08 20:51:04.336 10798-10827/com.readygo.rxjavademo D/RxJavaDemo: observable的工作线程为:RxCachedThreadScheduler-1
05-08 20:52:04.338 10798-10798/com.readygo.rxjavademo D/RxJavaDemo: observer---onNext--value:从磁盘缓存中获取数据为:RxJavaDemo--Test
05-08 20:52:04.340 10798-10798/com.readygo.rxjavademo D/RxJavaDemo: observer---onComplete
建议
建议大家仔细阅读一下:
Android RxJava:这是一份全面 & 详细 的RxJava操作符 使用攻略
https://www.jianshu.com/p/cd984dd5aae8
应该Android RxJava的使用就算是入门了.
参考资料
1.Android RxJava:这是一份全面 & 详细 的RxJava操作符 使用攻略
https://www.jianshu.com/p/cd984dd5aae8
2.Android RxJava 实际应用讲解:(无条件)网络请求轮询
https://www.jianshu.com/p/11b3ec672812
3.Android Rxjava:这是一篇 清晰 & 易懂的Rxjava 入门教程
https://www.jianshu.com/p/a406b94f3188
4.Android RxJava:细说 线程控制(切换 / 调度 )(含Retrofit实例讲解)
https://www.jianshu.com/p/5225b2baaecd