rxtest.java package com.example.rxtest;import android.graphics.Bitmap;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.View;import java.io.File;import rx.Observable;import
package com.example.rxtest; import android.graphics.Bitmap; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import java.io.File; import rx.Observable; import rx.Observer; import rx.Subscriber; import rx.android.schedulers.AndroidSchedulers; import rx.functions.Action1; import rx.functions.Func1; import rx.schedulers.Schedulers; public class MainActivity extends AppCompatActivity { File[] folders = {new File("/sdcard/a"), new File("/sdcard/b")}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Observable.just("Hello, world!").subscribe(s -> System.out.println(s + " -Dan")); } void old() { new Thread() { @Override public void run() { for (File folder : folders) { File[] files = folder.listFiles(); for (File file : files) { if (file.getName().endsWith(".png")) { //final Bitmap bitmap = getBitmapFromFile(file); runOnUiThread(new Runnable() { @Override public void run() { //imageCollectorView.addImage(bitmap); } }); } } } } }.start(); } void test1() { Observable.from(folders) .flatMap(new Func1>() { @Override public Observable call(File file) { return Observable.from(file.listFiles()); } }) .filter(file -> file.getName().endsWith(".png")) .map(file -> getBitmapFromFile(file)) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(bitmap -> { //imageCollectorView.addImage(bitmap); }); } private Bitmap getBitmapFromFile(File file) { return null; } private String tag = "qyg--->"; //创建Observer Observer observer0 = new Observer () { @Override public void onNext(String s) { Log.d(tag, "Item: " + s); } @Override public void onCompleted() { Log.d(tag, "Completed!"); } @Override public void onError(Throwable e) { Log.d(tag, "Error!"); } }; //创建observable Observable observable = Observable.create( (Observable.OnSubscribe ) subscriber1 -> { subscriber1.onNext("Hello"); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } subscriber1.onNext("Hi"); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } subscriber1.onNext("Aloha"); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } subscriber1.onCompleted(); }); //创建Subscriber Subscriber subscriber = new Subscriber () { @Override public void onNext(String s) { Log.d(tag, "Item: " + s); } @Override public void onCompleted() { Log.d(tag, "Completed!"); } @Override public void onError(Throwable e) { Log.d(tag, "Error!"); } }; Observable observable1 = Observable.just("Hello", "Hi", "Aloha"); // 将会依次调用: onNext("Hello"); onNext("Hi"); onNext("Aloha"); onCompleted(); String[] words = {"Hello", "Hi", "Aloha"}; Observable observable2 = Observable.from(words); // 将会依次调用: onNext("Hello"); onNext("Hi"); onNext("Aloha"); onCompleted(); public void click(View view) { observable.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(observer0); /*String[] names = {"hi", "ni", "hao", "me"}; Observable.from(names) .subscribe(new Action1 () { @Override public void call(String name) { Log.w(tag, name); } });*/ } }