Commit b34368cc authored by wanglei's avatar wanglei

...

parent 07d22166
Pipeline #1373 canceled with stages
package com.zxdemo.utils;
import android.os.Handler;
import android.os.Looper;
public class NonBlockingCountdown {
private static final long COUNTDOWN_INTERVAL = 1000; // 倒计时时间间隔,单位毫秒
private final Handler handler = new Handler(Looper.getMainLooper());
private Runnable countdownRunnable;
public NonBlockingCountdown(int s, TimeOut timeOut) {
this.countdownRunnable = new Runnable() {
int seconds = s;
@Override
public void run() {
if (seconds > 0) {
seconds--;
// 在这里更新UI,显示剩余时间
} else {
// 倒计时结束,可以在这里做一些操作,比如通知用户
handler.removeCallbacks(this);
timeOut.timeOut();
}
}
};
}
public void start() {
handler.postDelayed(countdownRunnable, COUNTDOWN_INTERVAL);
}
public void cancel() {
handler.removeCallbacks(countdownRunnable);
}
}
\ No newline at end of file
package com.zxdemo.utils;
interface TimeOut {
abstract void timeOut();
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment