Android 倒计时——Timer和CountDownTimer的使用,实现启动,暂停,继续,重复,重设时长以及启动service后台倒计时
生活随笔
收集整理的這篇文章主要介紹了
Android 倒计时——Timer和CountDownTimer的使用,实现启动,暂停,继续,重复,重设时长以及启动service后台倒计时
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
實現(xiàn)效果
單個倒計時功能? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?列表倒計時功能
???
?
自定義倒計時類
public class CountDownTimerSupport implements ITimerSupport {private Timer mTimer;private Handler mHandler;/*** 倒計時時間*/private long mMillisInFuture;/*** 間隔時間*/private long mCountDownInterval;/*** 倒計時剩余時間*/private long mMillisUntilFinished;private OnCountDownTimerListener mOnCountDownTimerListener;private TimerState mTimerState = TimerState.FINISH;@Deprecatedpublic CountDownTimerSupport() {this.mHandler = new Handler();}public CountDownTimerSupport(long millisInFuture, long countDownInterval) {this.setMillisInFuture(millisInFuture);this.setCountDownInterval(countDownInterval);this.mHandler = new Handler();}@Overridepublic void start() {//防止重復(fù)啟動 重新啟動要先reset再startif (mTimer == null && mTimerState != TimerState.START) {mTimer = new Timer();mTimer.scheduleAtFixedRate(createTimerTask(), 0, mCountDownInterval);mTimerState = TimerState.START;}}@Overridepublic void pause() {if (mTimer != null && mTimerState == TimerState.START) {cancelTimer();mTimerState = TimerState.PAUSE;}}@Overridepublic void resume() {if (mTimerState == TimerState.PAUSE) {start();}}@Overridepublic void stop() {if (mTimer != null) {cancelTimer();mMillisUntilFinished = mMillisInFuture;mTimerState = TimerState.FINISH;mHandler.post(new Runnable() {@Overridepublic void run() {if (mOnCountDownTimerListener != null) {mOnCountDownTimerListener.onFinish();}}});}}@Overridepublic void reset() {if (mTimer != null) {cancelTimer();}mMillisUntilFinished = mMillisInFuture;mTimerState = TimerState.FINISH;}private void cancelTimer() {mTimer.cancel();mTimer.purge();mTimer = null;}public boolean isStart() {return mTimerState == TimerState.START;}public boolean isFinish() {return mTimerState == TimerState.FINISH;}/*** @deprecated 使用構(gòu)造方法* @param millisInFuture*/@Deprecatedpublic void setMillisInFuture(long millisInFuture) {this.mMillisInFuture = millisInFuture;this.mMillisUntilFinished = mMillisInFuture;}/*** @deprecated 使用構(gòu)造方法* @param countDownInterval*/@Deprecatedpublic void setCountDownInterval(long countDownInterval) {this.mCountDownInterval = countDownInterval;}public void setOnCountDownTimerListener(OnCountDownTimerListener listener) {this.mOnCountDownTimerListener = listener;}public long getMillisUntilFinished() {return mMillisUntilFinished;}public TimerState getTimerState() {return mTimerState;}/*** @param millisInFuture* @param countDownInterval* @return* @deprecated 已更換Timer*/@Deprecatedprotected CountDownTimer createCountDownTimer(long millisInFuture, long countDownInterval) {return null;}protected TimerTask createTimerTask() {return new TimerTask() {private long startTime = -1;@Overridepublic void run() {if (startTime < 0) {//第一次回調(diào) 記錄開始時間startTime = scheduledExecutionTime() - (mMillisInFuture - mMillisUntilFinished);mHandler.post(new Runnable() {@Overridepublic void run() {if (mOnCountDownTimerListener != null) {mOnCountDownTimerListener.onTick(mMillisUntilFinished);}}});} else {//剩余時間mMillisUntilFinished = mMillisInFuture - (scheduledExecutionTime() - startTime);mHandler.post(new Runnable() {@Overridepublic void run() {if (mOnCountDownTimerListener != null) {mOnCountDownTimerListener.onTick(mMillisUntilFinished);}}});if (mMillisUntilFinished <= 0) {//如果沒有剩余時間 就停止stop();}}}};}}2、初始化
private CountDownTimerSupport mTimer; mTimer = new CountDownTimerSupport(duration * 1000, 1000);?
mTimer.setOnCountDownTimerListener(new OnCountDownTimerListener() {@Overridepublic void onTick(long millisUntilFinished) {tv.setText(millisUntilFinished + "ms\n" + millisUntilFinished / 1000 + "s");//倒計時 // textView.setText((60 * 1000 - millisUntilFinished) / 1000 + "S");//正計時Log.d("CountDownTimerSupport", "onTick : " + millisUntilFinished + "ms");}@Overridepublic void onFinish() {tv.setText("已停止");Log.d("CountDownTimerSupport", "onFinish");}});啟動
mTimer.start();暫停
mTimer.pause();繼續(xù)
mTimer.resume();停止
mTimer.stop();重復(fù)啟動,重設(shè)時長
mTimer.reset();//重復(fù)啟動 // mTimer.setMillisInFuture(30000);//重設(shè)時長mTimer.start();//重復(fù)啟動?
// 發(fā)送帶有數(shù)據(jù)的廣播 private void broadcastUpdate(final String action, String time) {final Intent intent = new Intent(action);intent.putExtra("time", time);sendBroadcast(intent); } // 發(fā)送廣播方法——更新倒計時broadcastUpdate(IN_RUNNING, time / 1000 + "");// 關(guān)閉服務(wù),停止倒計時Intent countDownIntent = new Intent(MainActivity.this, CodeTimerService.class);stopService(countDownIntent);finish();?
實現(xiàn)demo:?https://download.csdn.net/download/meixi_android/12484271
bug在線交流:QQ1085220040?
總結(jié)
以上是生活随笔為你收集整理的Android 倒计时——Timer和CountDownTimer的使用,实现启动,暂停,继续,重复,重设时长以及启动service后台倒计时的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基础回顾之泛型
- 下一篇: 用python延续科比的曼巴精神