Android开发RxJava 实现定时器

释放双眼,带上耳机,听听看~!
import com.h.t.DebugLogger;
import com.h.t.MtsAds;
import java.util.concurrent.TimeUnit;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import rx.android.schedulers.AndroidSchedulers;
/**
* description :Rxjava 定时工具类
* author : DaYu
* date : 2019-08-30 21:52
*/
public class RxTimeUtils {
private static Disposable mDisposable;
public static void timer(long milliseconds, final IRxNext next) {
Observable.timer(milliseconds, TimeUnit.MILLISECONDS)
//                .observeOn(AndroidSchedulers)
.subscribe(new Observer<Long>() {
@Override
public void onSubscribe(Disposable d) {
mDisposable = d;
}
@Override
public void onNext(Long aLong) {
if (next != null) {
next.doNext(aLong);
}
}
@Override
public void onError(Throwable e) {
cancel();
}
@Override
public void onComplete() {
}
});
}
public static void interval(long milliseconds, IRxNext next) {
Observable.interval(milliseconds, TimeUnit.MILLISECONDS)
//                .observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<Long>() {
@Override
public void onSubscribe(Disposable d) {
mDisposable = d;
}
@Override
public void onNext(Long aLong) {
if (next != null) {
next.doNext(aLong);
}
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
}
public static void cancel() {
if (mDisposable != null && !mDisposable.isDisposed()) {
mDisposable.dispose();
}
}
public interface IRxNext {
void doNext(long number);
}
}

用法

RxTimeUtils.timer(20 * 1000, new RxTimeUtils.IRxNext() {
@Override
public void doNext(long number) {
Log.e("home_show_three", "======MainActivity======" + number);
if (isFront && IsFirstStart) {
initHomeThreeAd();
}
}
});

人已赞赏
Android文章

Android开发ViewPage禁止抢焦点 禁止左右切换

2020-3-10 22:05:11

Android文章

Android中如何在代码中动态设置控件的高度以及宽度

2020-3-10 23:19:35

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索