大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺
public class CountDownTimerManager {
/** * 总倒计时 */
private long millisInFuture = 0;
/** * 回调时间 */
private long countDownInterval;
/** * 倒计时完成回调 */
private FinishCountDown finishCountDown;
/** * 定时完成回调 */
private TickCountDown tickCountDown;
private SingleCountDownTimer timer;
public static CountDownTimerManager get() {
return new CountDownTimerManager();
}
/** * 开始计时 */
public void start() {
if (null == timer) {
create();
}
timer.start();
}
/** * 结束计时 */
public void cancel() {
if (null != timer) {
timer.cancel();
}
}
/** * 销毁 */
public void destory() {
if (null != timer) {
timer.cancel();
}
timer = null;
}
/** * 设置总倒计时时间 * * @param millisInFuture 倒计时时间 * @return 本体 */
public CountDownTimerManager setMillisInFuture(long millisInFuture) {
this.millisInFuture = millisInFuture;
return this;
}
/** * 设置回调时间 * * @param countDownInterval 回调时间 * @return 本体 */
public CountDownTimerManager setCountDownInterval(long countDownInterval) {
this.countDownInterval = countDownInterval;
return this;
}
/** * 设置倒计时完成回调接口 * * @param finishCountDown 倒计时完成回调接口 * @return 本体 */
public CountDownTimerManager setFinishCountDown(FinishCountDown finishCountDown) {
this.finishCountDown = finishCountDown;
return this;
}
/** * 设置定时的回调接口 * * @param tickCountDown 定时的回调接口 * @return 本体 */
public CountDownTimerManager setTickCountDown(TickCountDown tickCountDown) {
this.tickCountDown = tickCountDown;
return this;
}
/** * 创建倒计时 */
private void create() {
if (null != timer) {
timer.cancel();
timer = null;
}
long defaultMillisInFuture = 3000;
long defaultCountDownInterval = 1000;
timer = new SingleCountDownTimer(millisInFuture <= 0 ? defaultMillisInFuture : millisInFuture, countDownInterval <= 0 ? defaultCountDownInterval : countDownInterval);
timer.setFinishCountDown(finishCountDown);
timer.setTickCountDown(tickCountDown);
}
/** * 倒计时完成回调接口 */
public interface FinishCountDown {
void onFinish();
}
/** * 定时的回调接口 */
public interface TickCountDown {
void onTick(long millisUntilFinished);
}
private static class SingleCountDownTimer extends CountDownTimer {
private FinishCountDown finishCountDown;
private TickCountDown tickCountDown;
public SingleCountDownTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
if (null != tickCountDown) {
tickCountDown.onTick(millisUntilFinished);
}
}
@Override
public void onFinish() {
if (null != finishCountDown) {
finishCountDown.onFinish();
}
}
public void setFinishCountDown(FinishCountDown finishCountDown) {
this.finishCountDown = finishCountDown;
}
public void setTickCountDown(TickCountDown tickCountDown) {
this.tickCountDown = tickCountDown;
}
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/192201.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...