笔记
倒计时
/*** 短信倒计时** @param s*/private Subscription subscription30;public void startCountdownFinishRx30(int s) {clearFinishSubscription30();subscription30 = Observable.interval(0, 1, TimeUnit.SECONDS).take(s + 1).map(new Func1<Long, Long>() {@Overridepublic Long call(Long aLong) {Log.d("FinishRx call", aLong + "");//这里是正序return s - aLong;}}).observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<Long>() {@Overridepublic void onCompleted() {Log.d("FinishRx onCompleted", "---------------");mDialog.dismiss();clearFinishSubscription30();}@Overridepublic void onError(Throwable e) {}@Overridepublic void onNext(Long aLong) {Log.d("FinishRx onNext", aLong + "");//这里是倒叙}});}/*** 关闭 倒计时退出*/public void clearFinishSubscription30() {if (subscription30 != null && !subscription30.isUnsubscribed()) {subscription30.unsubscribe();}}
**
轮询
**
private Subscription subscribeTime;/*** 當前頁面的定時器*/private void startTime() {closeTime();//5秒之后,发送,每隔10秒发送一次subscribeTime = Observable.interval(5, 10, TimeUnit.SECONDS).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Long>() {@Overridepublic void call(Long aLong) {if (aLong % 30 == 0) {getWeatherWrapper();}//时间getTime();//5分钟请求一次。if (aLong % 300 == 0) {getBanner();}if (aLong % 65 == 0) {getTestPresenter.HeartbeatPostObservable();refreshTheAdvertisement();}if (aLong % 5 == 0) {if (!SysRes.isConnected(getContext())) {mViewAnimator.setDisplayedChild(2);L.i(TAG_ERROR, "------设置页面网络异常--4----");}}}});}/*** 关闭时间监听*/private void closeTime() {if (null != subscribeTime && !subscribeTime.isUnsubscribed()) {subscribeTime.unsubscribe();subscribeTime = null;}}