public class TimeBean {
private String day;
private String hour;
private String minute;
private String second;
// ... getter and setter
}
public class SchedulerBean {
private ArrayList list;
private String name;
private String org;
// ... getter and setter
}
监听当前时间的变化,并且更新到view上,频率为一秒一次
// 外面有一个timeBean变量,每一秒发送一次
Observable observable = Observable.create(new ObservableOnSubscribe() {
@Override
public void subscribe(@NonNull ObservableEmitter observableEmitter) throws Exception {
observableEmitter.onNext(timeBean);
}
});
mObserver = new Observer() {
@Override
public void onSubscribe(@NonNull Disposable d) {
}
@Override
public void onNext(@NonNull TimeBean timeBean) {
currentHourView.setText(timeBean.getHour());
currentMinuteView.setText(timeBean.getMinute());
currentSecondView.setText(timeBean.getSecond());
}
@Override
public void onError(@NonNull Throwable e) {
}
@Override
public void onComplete() {
}
};
observable.subscribeOn(AndroidSchedulers.mainThread()).subscribe(mObserver);
注意:subscribeOn方法决定了观察者执行时所在的线程,由于要更新view,所以一定要放在Android的主线程,如果没设置,或者设置为Schedulers.io、Schedulers.newThread,都无法更新数据到view,而且也没有报错。
使用Schedulers.io时,即将操作放在了非主线程上,但是每次使用时是在同个线程,而使用Schedulers.newThread时则每次都会新建一个线程
使用Timer对象实现周期为一秒的循环,每一秒都创建一个Observable,并发送当前时间
timeTask = new TimerTask() {
@Override
public void run() {
Calendar now = Calendar.getInstance();
TimeBean timeBean = new TimeBean();
timeBean.setDay(getIntString(now.get(Calendar.DAY_OF_MONTH)));
timeBean.setHour(getIntString(now.get(Calendar.HOUR_OF_DAY)));
timeBean.setMinute(getIntString(now.get(Calendar.MINUTE)));
timeBean.setSecond(getIntString(now.get(Calendar.SECOND)));
createObservable(timeBean);
}
};
createObservable方法中,创建observable对象,并执行subscribe方法。
主要功能是,使用一个初始的Observable对象发送一个包含列表的对象,再将列表从对象中取出,再发送一次,然后将列表中的每个item取出再发送一次,使用到三个Observer对象。
这个对象是一个SchedulerBean的实体,包含一个列表,列表中的每个item都是一个TimeBean实体。
final SchedulerBean bean = new SchedulerBean();
for (int i = 0; i < 10; i++) {
TimeBean timeBean = new TimeBean();
timeBean.setMinute(getIntString(22));
timeBean.setHour(getIntString(17 + i));
timeBean.setSecond(getIntString(6));
bean.addTime(timeBean);
}
创建一个初始的Observable,对发送的对象进行不同的处理,然后分别发送给不同的观察者
Observable observable = Observable.create(new ObservableOnSubscribe() {
@Override
public void subscribe(@NonNull ObservableEmitter e) throws Exception {
e.onNext(bean);
}
}).subscribeOn(Schedulers.io());
// 不进行处理直接发送
observable.subscribe(schedulerObserver);
// 取出列表然后发送
observable.map(new Function() {
@Override
public ArrayList apply(@NonNull SchedulerBean schedulerBean) throws Exception {
return schedulerBean.getList();
}
}).subscribe(listObserver);
// 取出列表中的每一个元素,再发送
observable.flatMap(new Function>() {
@Override
public ObservableSource apply(@NonNull SchedulerBean schedulerBean) throws Exception {
return Observable.fromIterable(schedulerBean.getList());
}
}).subscribe(timeBeanObserver);
初始对象的观察者:
Observer schedulerObserver = new Observer() {
@Override
public void onSubscribe(@NonNull Disposable d) {
}
@Override
public void onNext(@NonNull SchedulerBean schedulerBean) {
log("---- schedulerObserver ----");
}
@Override
public void onError(@NonNull Throwable e) {
}
@Override
public void onComplete() {
}
};
列表的观察者:
Observer listObserver = new Observer() {
@Override
public void onSubscribe(@NonNull Disposable d) {
}
@Override
public void onNext(@NonNull ArrayList list) {
log("the size of list = "+list.size());
}
@Override
public void onError(@NonNull Throwable e) {
}
@Override
public void onComplete() {
}
};
列表元素的观察者:
Observer timeBeanObserver = new Observer() {
@Override
public void onSubscribe(@NonNull Disposable d) {
}
@Override
public void onNext(@NonNull TimeBean timeBean) {
log("timeBeanObserver = " + timeBean.getHour());
}
@Override
public void onError(@NonNull Throwable e) {
}
@Override
public void onComplete() {
}
};
---- schedulerObserver --- //未加工的被发送对象
the size of list = 10 //从初始被发送对象中取出的列表
timeBeanObserver = 17 //取出的列表元素
timeBeanObserver = 18
timeBeanObserver = 19
timeBeanObserver = 20
timeBeanObserver = 21
timeBeanObserver = 22
timeBeanObserver = 23
timeBeanObserver = 24
timeBeanObserver = 25
timeBeanObserver = 26