简单例子
package com.rx_android;
import android.util.Log;
import java.util.concurrent.TimeUnit;
import rx.Observable;
import rx.Subscriber;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;
/**
* Created by Administrator on 2016/8/5.
*/
public class RxUtils {
private static final String TAG = RxUtils.class.getSimpleName();
//使用create方式
public static void createObserable(){
//定义被观察者
Observable<String> observable = Observable.create(new Observable.OnSubscribe<String>() {
@Override
public void call(Subscriber<? super String> subscriber) {
if(!subscriber.isUnsubscribed()){
subscriber.onNext("hello");
subscriber.onNext("hi");
subscriber.onNext(downLoadJson());
subscriber.onNext("world");
subscriber.onCompleted();
}
}
});
Subscriber<String> showsub = new Subscriber<String>() {
@Override
public void onCompleted() {
Log.i(TAG,"completed");
}
@Override
public void onError(Throwable e) {
Log.i(TAG,e.getMessage());
}
@Override
public void onNext(String s) {
Log.i(TAG,"result--->>"+s);
}
};
observable.subscribe(showsub);
}
private static String downLoadJson() {
return "json data!";
}
/**
* 第二种方式,其实就是第一种的缩写
*
*/
public static void createPrint(){
Observable.create(new Observable.OnSubscribe<Integer>() {
@Override
public void call(Subscriber<? super Integer> subscriber) {
if(!subscriber.isUnsubscribed()){
for(int i = 1; i <=10;i++){
subscriber.onNext(i);
}
subscriber.onCompleted();
}
}
}).subscribe(new Subscriber<Integer>() {
@Override
public void onCompleted() {
Log.i(TAG,"completed");
}
@Override
public void onError(Throwable e) {
Log.i(TAG,e.getMessage());
}
@Override
public void onNext(Integer integer) {
Log.i(TAG,"result--->>"+integer);
}
});
}
/**
* 使用在被观察者,返回的对象一般都是数值类型
*
*/
public static void from(){
Integer [] items = {1,2,3,4,5,6,7,8,9};
Observable observable = Observable.from(items);
observable.subscribe(new Action1() {
@Override
public void call(Object o) {
Log.i(TAG, o.toString());
}
});
}
/**
* 指定某一时刻进行数据发送
*
*/
public static void interval(){
Integer[] items = {1, 2, 3, 4, 5};
Observable observable = Observable.interval(1, 1, TimeUnit.SECONDS);//每一个发送数据
observable.subscribe(new Action1() {
@Override
public void call(Object o) {
Log.i(TAG, o.toString());
}
});
}
/**
* 处理数组集合
*
*/
public static void just(){
Integer[] items1 = {1,2,3,4,5,6};
Integer[] items2 = {5,7,6,7,8,2,6,};
Observable observable = Observable.just(items1, items2);
observable.subscribe(new Subscriber<Integer[]>() {
@Override
public void onCompleted() {
Log.i(TAG, "onCompleted");
}
@Override
public void onError(Throwable e) {
Log.i(TAG, e.getMessage());
}
@Override
public void onNext(Integer[] integers) {
for(int i=0;i<integers.length;i++){
Log.i(TAG, "next:" + integers[i]);
}
}
});
}
/**
* 使用范围数据,指定输出数据的范围
*
*/
public static void range(){
Observable observable = Observable.range(1,40);
observable.subscribe(new Subscriber() {
@Override
public void onCompleted() {
Log.i(TAG, "onCompleted");
}
@Override
public void onError(Throwable e) {
Log.i(TAG, e.getMessage());
}
@Override
public void onNext(Object o) {
Log.i(TAG,"next:"+o);
}
});
}
/**
*
* 使用过滤工程
*
*/
public static void filter(){
Observable observable = Observable.just(1,2,3,4,5,6,7,8,9,10);
observable.filter(new Func1<Integer,Boolean>(){
@Override
public Boolean call(Integer integer) {
return integer<5;
}
}).observeOn(Schedulers.io()).subscribe(new Subscriber<Integer>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(Integer integer) {
Log.i(TAG,integer.toString());
}
});
};
}
布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Create方法"
android:id="@+id/button"
android:layout_gravity="center_horizontal"
android:onClick="createMethod"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Create Print 方法"
android:id="@+id/button2"
android:layout_gravity="center_horizontal"
android:onClick="createPrint"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="from 方法"
android:id="@+id/button3"
android:onClick="from"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Interval 方法"
android:id="@+id/button4"
android:onClick="interval"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Just 方法"
android:id="@+id/button5"
android:onClick="just"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="range 方法"
android:id="@+id/button6"
android:onClick="range"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="filter 方法"
android:id="@+id/button7"
android:onClick="filter"
/>
</LinearLayout>
package com.example.administrator.myapplication;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import com.rx_android.RxUtils;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void createMethod(View view){
RxUtils.createObserable();
}
public void createPrint(View view){
RxUtils.createPrint();
}
public void from(View view){
RxUtils.from();
}
public void interval(View view){
RxUtils.interval();
}
public void just(View view){
RxUtils.just();
}
public void range(View view){
RxUtils.range();
}
public void filter(View view){
RxUtils.filter();
}
}