实现效果

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

   

自定义倒计时类

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() {//防止重复启动 重新启动要先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 使用构造方法* @param millisInFuture*/@Deprecatedpublic void setMillisInFuture(long millisInFuture) {this.mMillisInFuture = millisInFuture;this.mMillisUntilFinished = mMillisInFuture;}/*** @deprecated 使用构造方法* @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) {//第一次回调 记录开始时间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();

继续

mTimer.resume();

停止

mTimer.stop();

重复启动,重设时长

            mTimer.reset();//重复启动
//            mTimer.setMillisInFuture(30000);//重设时长mTimer.start();//重复启动
// 发送带有数据的广播
private void broadcastUpdate(final String action, String time) {final Intent intent = new Intent(action);intent.putExtra("time", time);sendBroadcast(intent);
}
//        发送广播方法——更新倒计时broadcastUpdate(IN_RUNNING, time / 1000 + "");//        关闭服务,停止倒计时Intent countDownIntent = new Intent(MainActivity.this, CodeTimerService.class);stopService(countDownIntent);finish();

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

bug在线交流:QQ1085220040

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

  1. 倒计时 总结 Timer Handler CountDownTimer RxJava MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  2. Android实现倒计时之使用CountDownTimer

    在开发中会经常用到倒计时这个功能,包括给手机发送验证码等等,之前我的做法都是使用Handler + Timer + TimerTask来实现,现在发现了这个类,果断抛弃之前的做法,相信还是有很多人和我 ...

  3. android中倒计时控件CountDownTimer分析

    android中倒计时控件CountDownTimer分析1 示例代码 new CountDownTimer(10000, 1000) {public void onTick(long millisU ...

  4. android倒计时停止,Android 使用 Timer 做倒计时。实现开始 (start),取消 (cancel),暂停 (pause),重开 (resume)功能...

    在 Android 使用 Timer 做倒计时.实现开始 (start),取消 (cancel),暂停 (pause),恢复 (resume)功能 在 Android 开发中,我们很多地方会使用到倒计 ...

  5. Android倒计时解决方案之CountDownTimer

    CountDownTimer 构造函数: CountDownTimer (long millisInFuture, long countDownInterval) millisInfuture: 要倒 ...

  6. android的timertask,Android 中 Timer 和 TimerTask的使用

    Android中Timer是一个普通的类,其中有几个重要的方法:而TimerTask则是一个抽象类,其中含有一个抽象方法run(). 使用Timer类中的schedule()方法可以完成对TimerT ...

  7. Android定时器Timer简单使用

    Android定时器Timer简单使用 Timer简介 Timer使用 总结 Timer简介 Timer(计时器)位于 java.util包下,可用于创建定时任务,任务可以安排为一次性执行,也可以定期 ...

  8. html倒计时timer,js如何使用定时器实现倒计时功能

    这次给大家带来js如何使用定时器实现倒计时功能,js使用定时器实现倒计时功能的注意事项有哪些,下面就是实战案例,一起来看一下. 日期函数 倒计时 = 用 将来的时间 - 现在的时间 问题:将来时间 距 ...

  9. Android Toast 自定义显示时长

    Android Toast 只支持两种时间 LENGTH_SHORT 2 秒,LENGTH_LONG 3.5 秒,但是有场景需要自定义显示时长就会有问题,所以需要自定义实现,下边是自定义的类,通过定时 ...

最新文章

  1. Go 知识点(08) — 对未初始化的 channel 进行读写操作
  2. pyautogui 的用法 python自动操作鼠标、键盘
  3. apache vhost
  4. 让Android Studio代码提示不区分大小写的方法
  5. zabbix监控apache
  6. 【译】Build Knowledge Graph from unstructured corpus using Machine Learning
  7. Linux之Centos允许root连接ftp
  8. 01-复杂度2 Maximum Subsequence Sum (25 分)
  9. spring mvc 实现单文件 || 多文件上传
  10. (STTN)Learning Joint Spatial-TemporalTransformations for Video Inpainting
  11. 计算机硬盘驱动器可以存软件吗,如何从计算机硬盘驱动器恢复数据
  12. 汽车硬件测试基准介绍
  13. 26. 平衡二叉排序树
  14. 利用uDig 快速生成 GeoServer 可用的 SLD 渲染文件
  15. python学完基础后实践练习:阿拉伯数字金额转人名币大写
  16. dedecms源码分析(第一部分)
  17. 单位公司年会团拜会抽奖软件程序
  18. greenplum数据导入导出
  19. 使用xbox游戏手柄控制PX4的gazebo仿真
  20. 推荐一首歌 - Just Another (Pete Yorn)

热门文章

  1. [css] 你有使用过字体图标吗?它有什么好处?
  2. 前端学习(1885)vue之电商管理系统电商系统之首页路由的重定向
  3. jenkins安装(1)
  4. java quartz 2.2.3_java – Spring 3 Quartz 2错误
  5. Vue之实例的生命周期
  6. 2018 总结 2019 展望
  7. dw网页设计期末设计一个网页_Dreamweaver网页设计期末模拟试题(1)
  8. python输入n×n的矩阵0和1_关于Python数组和矩阵的用法X[:,0]、X[:,1]、X[:,:,0]、X[:,:,1]、X[:,m:n]和X[:,:,m:n]...
  9. eclipse debug怎么用_利用maven/eclipse搭建ssm(spring+spring mvc+mybatis)
  10. [Luogu] P1939 【模板】矩阵加速(数列)