当前位置:   article > 正文

Android 倒计时——Timer和CountDownTimer的使用,实现启动,暂停,继续,重复,重设时长以及启动service后台倒计时_countdowntimer 会重叠

countdowntimer 会重叠

实现效果

单个倒计时功能                                                                                 列表倒计时功能

   

 

自定义倒计时类

  1. public class CountDownTimerSupport implements ITimerSupport {
  2. private Timer mTimer;
  3. private Handler mHandler;
  4. /**
  5. * 倒计时时间
  6. */
  7. private long mMillisInFuture;
  8. /**
  9. * 间隔时间
  10. */
  11. private long mCountDownInterval;
  12. /**
  13. * 倒计时剩余时间
  14. */
  15. private long mMillisUntilFinished;
  16. private OnCountDownTimerListener mOnCountDownTimerListener;
  17. private TimerState mTimerState = TimerState.FINISH;
  18. @Deprecated
  19. public CountDownTimerSupport() {
  20. this.mHandler = new Handler();
  21. }
  22. public CountDownTimerSupport(long millisInFuture, long countDownInterval) {
  23. this.setMillisInFuture(millisInFuture);
  24. this.setCountDownInterval(countDownInterval);
  25. this.mHandler = new Handler();
  26. }
  27. @Override
  28. public void start() {
  29. //防止重复启动 重新启动要先reset再start
  30. if (mTimer == null && mTimerState != TimerState.START) {
  31. mTimer = new Timer();
  32. mTimer.scheduleAtFixedRate(createTimerTask(), 0, mCountDownInterval);
  33. mTimerState = TimerState.START;
  34. }
  35. }
  36. @Override
  37. public void pause() {
  38. if (mTimer != null && mTimerState == TimerState.START) {
  39. cancelTimer();
  40. mTimerState = TimerState.PAUSE;
  41. }
  42. }
  43. @Override
  44. public void resume() {
  45. if (mTimerState == TimerState.PAUSE) {
  46. start();
  47. }
  48. }
  49. @Override
  50. public void stop() {
  51. if (mTimer != null) {
  52. cancelTimer();
  53. mMillisUntilFinished = mMillisInFuture;
  54. mTimerState = TimerState.FINISH;
  55. mHandler.post(new Runnable() {
  56. @Override
  57. public void run() {
  58. if (mOnCountDownTimerListener != null) {
  59. mOnCountDownTimerListener.onFinish();
  60. }
  61. }
  62. });
  63. }
  64. }
  65. @Override
  66. public void reset() {
  67. if (mTimer != null) {
  68. cancelTimer();
  69. }
  70. mMillisUntilFinished = mMillisInFuture;
  71. mTimerState = TimerState.FINISH;
  72. }
  73. private void cancelTimer() {
  74. mTimer.cancel();
  75. mTimer.purge();
  76. mTimer = null;
  77. }
  78. public boolean isStart() {
  79. return mTimerState == TimerState.START;
  80. }
  81. public boolean isFinish() {
  82. return mTimerState == TimerState.FINISH;
  83. }
  84. /**
  85. * @deprecated 使用构造方法
  86. * @param millisInFuture
  87. */
  88. @Deprecated
  89. public void setMillisInFuture(long millisInFuture) {
  90. this.mMillisInFuture = millisInFuture;
  91. this.mMillisUntilFinished = mMillisInFuture;
  92. }
  93. /**
  94. * @deprecated 使用构造方法
  95. * @param countDownInterval
  96. */
  97. @Deprecated
  98. public void setCountDownInterval(long countDownInterval) {
  99. this.mCountDownInterval = countDownInterval;
  100. }
  101. public void setOnCountDownTimerListener(OnCountDownTimerListener listener) {
  102. this.mOnCountDownTimerListener = listener;
  103. }
  104. public long getMillisUntilFinished() {
  105. return mMillisUntilFinished;
  106. }
  107. public TimerState getTimerState() {
  108. return mTimerState;
  109. }
  110. /**
  111. * @param millisInFuture
  112. * @param countDownInterval
  113. * @return
  114. * @deprecated 已更换Timer
  115. */
  116. @Deprecated
  117. protected CountDownTimer createCountDownTimer(long millisInFuture, long countDownInterval) {
  118. return null;
  119. }
  120. protected TimerTask createTimerTask() {
  121. return new TimerTask() {
  122. private long startTime = -1;
  123. @Override
  124. public void run() {
  125. if (startTime < 0) {
  126. //第一次回调 记录开始时间
  127. startTime = scheduledExecutionTime() - (mMillisInFuture - mMillisUntilFinished);
  128. mHandler.post(new Runnable() {
  129. @Override
  130. public void run() {
  131. if (mOnCountDownTimerListener != null) {
  132. mOnCountDownTimerListener.onTick(mMillisUntilFinished);
  133. }
  134. }
  135. });
  136. } else {
  137. //剩余时间
  138. mMillisUntilFinished = mMillisInFuture - (scheduledExecutionTime() - startTime);
  139. mHandler.post(new Runnable() {
  140. @Override
  141. public void run() {
  142. if (mOnCountDownTimerListener != null) {
  143. mOnCountDownTimerListener.onTick(mMillisUntilFinished);
  144. }
  145. }
  146. });
  147. if (mMillisUntilFinished <= 0) {
  148. //如果没有剩余时间 就停止
  149. stop();
  150. }
  151. }
  152. }
  153. };
  154. }
  155. }

2、初始化

private CountDownTimerSupport mTimer;
 mTimer = new CountDownTimerSupport(duration * 1000, 1000);

 

  1. mTimer.setOnCountDownTimerListener(new OnCountDownTimerListener() {
  2. @Override
  3. public void onTick(long millisUntilFinished) {
  4. tv.setText(millisUntilFinished + "ms\n" + millisUntilFinished / 1000 + "s");//倒计时
  5. // textView.setText((60 * 1000 - millisUntilFinished) / 1000 + "S");//正计时
  6. Log.d("CountDownTimerSupport", "onTick : " + millisUntilFinished + "ms");
  7. }
  8. @Override
  9. public void onFinish() {
  10. tv.setText("已停止");
  11. Log.d("CountDownTimerSupport", "onFinish");
  12. }
  13. });

启动

mTimer.start();

暂停

mTimer.pause();

继续

mTimer.resume();

停止

mTimer.stop();

重复启动,重设时长

  1. mTimer.reset();//重复启动
  2. // mTimer.setMillisInFuture(30000);//重设时长
  3. mTimer.start();//重复启动

 

  1. // 发送带有数据的广播
  2. private void broadcastUpdate(final String action, String time) {
  3. final Intent intent = new Intent(action);
  4. intent.putExtra("time", time);
  5. sendBroadcast(intent);
  6. }
  1. // 发送广播方法——更新倒计时
  2. broadcastUpdate(IN_RUNNING, time / 1000 + "");
  3. // 关闭服务,停止倒计时
  4. Intent countDownIntent = new Intent(MainActivity.this, CodeTimerService.class);
  5. stopService(countDownIntent);
  6. finish();

 

实现demo: https://download.csdn.net/download/meixi_android/12484271

bug在线交流:QQ1085220040 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/808962
推荐阅读
相关标签
  

闽ICP备14008679号