package com.example.demorxjava;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import rx.Observable;
import rx.Subscriber;
public class SimpleRxjavaActivity extends AppCompatActivity {
private Observable<String> mObservable;
private Subscriber<String> mSubScriber;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//一。可观察对象产生数据
//call当产生订阅关系
//-->只机注册的观察者
//结束
mObservable = Observable.create(new Observable.OnSubscribe<String>() {
//call当产生订阅关系
@Override
public void call(Subscriber<? super String> subscriber) {
subscriber.onNext("this is message");//-->只机注册的观察者
int value=1/0;//-->异常事件 交给onError
subscriber.onCompleted();//结束
}
});
//二。观察者 接收数据事件 一定订阅
/*接收完成事件*//*处理出错*//*接收更新数据*/
mSubScriber = new Subscriber<String>() {
/*接收完成事件*/
@Override
public void onCompleted() {
ToastUtil.showShort(getApplicationContext(),"接收完成事件onCompleted");
}
/*处理出错*/
@Override
public void onError(Throwable e) {
ToastUtil.showShort(getApplicationContext(),"接收异常处理e"+e.getMessage());
}
/*接收更新数据*/
@Override
public void onNext(String data) {
ToastUtil.showShort(getApplicationContext(),"接收onNext事件 "+data);
}
};
}
public void start(View view)
{
mObservable.subscribe(mSubScriber);//-->数据改变
}
}