轉(zhuǎn)自:http://ranseti.top/article/rxandroid
RxAndroid是開(kāi)源項(xiàng)目集合ReactiveX中用于平臺(tái)和框架的一個(gè)開(kāi)源庫(kù)耙考,GitHub地址https://github.com/ReactiveX/RxAndroid。使用RxAndroid需要依賴于Rxjava潭兽。
該模塊將最小類添加到RxJava中倦始,使得在Android應(yīng)用程序中編寫(xiě)反應(yīng)式組件變得簡(jiǎn)單而輕松。 更具體地說(shuō)山卦,它提供了一個(gè)調(diào)度程序在主線程或任何給定的Looper上進(jìn)行調(diào)度鞋邑。
Observing on the main thread(在主線上觀察)
One of the most common operations when dealing with asynchronous tasks on Android is to observe the task's result or outcome on the main thread. Using vanilla Android, this would typically be accomplished with an AsyncTask. With RxJava instead you would declare your Observable to be observed on the main thread:
(在Android上處理異步任務(wù)時(shí)最常見(jiàn)的操作之一是在主線程上觀察任務(wù)的結(jié)果或結(jié)果。 使用vanilla Android怒坯,通常使用AsyncTask來(lái)完成炫狱。 用RxJava代替你會(huì)在主線程中聲明你的Observable:
Observable.just("one", "two", "three", "four", "five")
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(/* an Observer */);
This will execute the Observable on a new thread, and emit results through onNext on the main thread.
(這將在新線程上執(zhí)行Observable,并通過(guò)主線程上的onNext發(fā)送結(jié)果剔猿。)
Observing on arbitrary loopers(在任意的loopers上觀察)
The previous sample is merely a specialization of a more general concept: binding asynchronous communication to an Android message loop, or Looper. In order to observe an Observable on an arbitrary Looper, create an associated Scheduler by calling AndroidSchedulers.from:
(前面的示例僅僅是一個(gè)更一般概念的專門(mén)化:將異步通信綁定到Android消息循環(huán)或Looper视译。 為了在一個(gè)任意的Looper上觀察一個(gè)Observable,通過(guò)調(diào)用AndroidSchedulers.from創(chuàng)建一個(gè)關(guān)聯(lián)的Scheduler:)
Looper backgroundLooper = // ...
Observable.just("one", "two", "three", "four", "five")
.observeOn(AndroidSchedulers.from(backgroundLooper))
.subscribe(/* an Observer */)
This will execute the Observable on a new thread and emit results through onNext on whatever thread is running backgroundLooper.
(這將在一個(gè)新的線程上執(zhí)行Observable归敬,并在任何運(yùn)行backgroundLooper的線程上通過(guò)onNext發(fā)送結(jié)果酷含。)
看完RxAndroid官方介紹后對(duì)RxAndroid和RxJava的認(rèn)識(shí)還是比較模糊,不過(guò)可以看看朱凱大神的解析http://gank.io/post/560e15be2dca930e00da1083汪茧。