是简单的实现一个动态显示时间的需求。使用RxJava
private Disposable subscription;
subscription = Observable.interval(0, 1, TimeUnit.SECONDS)
.map(new Function<Long, Long>() {
@Override
public Long apply(Long aLong) throws Exception {
return aLong;
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<Long>() {
@Override
public void accept(Long aLong) throws Exception {
tvHour.setText(DateUtils.getHour(System.currentTimeMillis()));
}
});
@Override
public void onDestroyView() {
super.onDestroyView();
if (!subscription.isDisposed()){
subscription.dispose();
}
}
网友评论