/**
* Schedule a countdown until a time in the future, with regular notifications on intervals along the way.
*
* Example of showing a 30 second countdown in a text field:
*
* new CountDownTimer(30000, 1000) {
*
* public void onTick(long millisUntilFinished) {
* mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
* }
*
* public void onFinish() {
* mTextField.setText("done!");
* }
* }.start();
*
* The calls to onTick(long) are synchronized to this object so that one call to onTick(long) won't ever occur before the previous
* callback is complete.
*
* This is only relevant when the implementation of onTick(long) takes an amount of time to execute that is significant compared to
* the countdown interval.
*/
public abstract class CountDownTimer { /**
* Millis since epoch when alarm should stop.
*/
private final long mMillisInFuture; /**
* The interval in millis that the user receives callbacks
*/
private final long mCountdownInterval; private long mStopTimeInFuture; /**
* @param millisInFuture The number of millis in the future from the call
* to {@link #start()} until the countdown is done and {@link #onFinish()}
* is called.
* @param countDownInterval The interval along the way to receive
* {@link #onTick(long)} callbacks.
*/
public CountDownTimer(long millisInFuture, long countDownInterval) {
mMillisInFuture = millisInFuture;
mCountdownInterval = countDownInterval;
} /**
* Cancel the countdown.
*/
public final void cancel() {
mHandler.removeMessages(MSG);
} /**
* Start the countdown.
*/
public synchronized final CountDownTimer start() {
if (mMillisInFuture <= 0) {
onFinish();
return this;
}
mStopTimeInFuture = SystemClock.elapsedRealtime() + mMillisInFuture;
mHandler.sendMessage(mHandler.obtainMessage(MSG));
return this;
} /**
* Callback fired on regular interval.
* @param millisUntilFinished The amount of time until finished.
*/
public abstract void onTick(long millisUntilFinished); /**
* Callback fired when the time is up.
*/
public abstract void onFinish(); private static final int MSG = 1; // handles counting down
private Handler mHandler = new Handler() { @Override
public void handleMessage(Message msg) { synchronized (CountDownTimer.this) {
final long millisLeft = mStopTimeInFuture - SystemClock.elapsedRealtime(); if (millisLeft <= 0) {
onFinish();
} else if (millisLeft < mCountdownInterval) {
// no tick, just delay until done
sendMessageDelayed(obtainMessage(MSG), millisLeft);
} else {
long lastTickStart = SystemClock.elapsedRealtime();
onTick(millisLeft); // take into account user's onTick taking time to execute
long delay = lastTickStart + mCountdownInterval - SystemClock.elapsedRealtime(); // special case: user's onTick took more than interval to complete, skip to next interval
while (delay < 0) delay += mCountdownInterval; sendMessageDelayed(obtainMessage(MSG), delay);
}
}
}
};
}

代码艺术 CountDownTimer的更多相关文章

  1. Linux Kernel代码艺术——系统调用宏定义

    我们习惯在SI(Source Insight)中阅读Linux内核,SI会建立符号表数据库,能非常方便地跳转到变量.宏.函数等的定义处.但在处理系统调用的函数时,却会遇到一些麻烦:我们知道系统调用函数 ...

  2. Linux Kernel代码艺术——数组初始化

    前几天看内核中系统调用代码,在系统调用向量表初始化中,有下面这段代码写的让我有点摸不着头脑: const sys_call_ptr_t sys_call_table[__NR_syscall_max+ ...

  3. Linux Kernel 代码艺术——编译时断言

    本系列文章主要写我在阅读Linux内核过程中,关注的比较难以理解但又设计巧妙的代码片段(不关注OS的各个模块的设计思想,此部分我准备写在“深入理解Linux Kernel” 系列文章中),一来通过内核 ...

  4. 关于web前端代码艺术

    以前一直都以为html代码要分离得很好,html一个文件,css一个文件,js一个文件,然后最好一个html页面里面不要要太多冗余的代码,不要恶心地引入一个又一个的js,连jquery的引入我都觉得有 ...

  5. Linux Kernel 代码艺术——编译时断言【转】

    转自:http://www.cnblogs.com/hazir/p/static_assert_macro.html 本系列文章主要写我在阅读Linux内核过程中,关注的比较难以理解但又设计巧妙的代码 ...

  6. Linux Kernel代码艺术——数组初始化【转】

    转自:http://www.cnblogs.com/hazir/p/array_initialization.html 前几天看内核中系统调用代码,在系统调用向量表初始化中,有下面这段代码写的让我有点 ...

  7. .NET - 代码重构技巧

    通过面向对象三大特性:封装.继承.多态的学习,可以说我们已经掌握了面向对象的核心.接下来的学习就是如何让我们的代码更优雅.更高效.更易读.更易维护.当然了,这也是从一个普通程序员到一个高级程序员的必由 ...

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

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

  9. Html5游戏开发-145行代码完成一个RPG小Demo

    lufy前辈写过<[代码艺术]17行代码的贪吃蛇小游戏>一文,忽悠了不少求知的兄弟进去阅读,阅读量当然是相当的大.今天我不仿也搞一个这样的教程,目地不在于忽悠人,而在于帮助他人. 先看de ...

随机推荐

  1. JS继承的6种方法

    1.原型链 基本思想:利用原型让一个引用类型继承另外一个引用类型的属性和方法. 构造函数,原型,实例之间的关系:每个构造函数都有一个原型对象,原型对象包含一个指向构造函数的指针,而实例都包含一个指向原 ...

  2. Oracle之批量生成数据

    一.引言 由于测试程序,需要大量的数据 二.方法 1.pl/sql的Generate Data,在tool菜单中可以找到,但是我这里不能用,老是出现错误,应该是软件的原因,但是没找到解决办法,如下图: ...

  3. Java Drp项目实战——Web应用server

    引言 Web应用server如今非常多人都在用,但是究竟什么是Web应用server呢,它与Webserver有什么关系,它与应用server又是什么关系,它是他们两种中的当中一种,还是简单的两种se ...

  4. Linux网络实时监控配置

    Linux监控邮件发送配置 网络状态监控 网络状态:netstat 各个状态的总计,详情:以及重点端口的详细连接情况(22,25,80,3306,8080),打印客户端连接数最多的ip. 邮件报告当前 ...

  5. FOUC - Flash Of Unstyled Content 文档样式闪烁

      问题描述 偶然间看到FOUC这个单词,在Google里找了半天终于发现了它的含义:Flash Of Unstyled Content.它指的是在某些情况下,IE在加载网页时会出现短暂的CSS样式失 ...

  6. Oracle常用命令大全

    一.ORACLE的启动和关闭 1.在单机环境下 要想启动或关闭ORACLE系统必须首先切换到ORACLE用户,如下 su - oracle a.启动ORACLE系统 oracle>svrmgrl ...

  7. 假设web应用的文档根目录为MyApp,那么可以从哪里找到database.jar文件。

    假设web应用的文档根目录为MyApp,那么可以从哪里找到database.jar文件. A. MyApp目录下 B. MyApp\images目录下 C. MyApp\WEB-INF目录下 D. M ...

  8. redux sample with redux source code

    code sample没有package.json文件,也就没有任何外部依赖,直接使用redux source code. nodejs对es6的import export还不支持,这里使用了stac ...

  9. 【watcher】 #02 c# 中实现时间戳等,日期数字及大概率绝对随机数 实现

    在Wacher的项目中,用到了很多时间记录的地方,为了将来能够和在线数据打通,我们使用了时间戳来记录时间信息 由于c# 没有现成的方法,所以我们重新写了一个Helper类来帮助我们使用这些公共函数 同 ...

  10. git & github 菜鸟笔记

    1.概念: 最先进的分布式版本控制系统 文件修改该提交的内容:---版本 文件名 用户 说明 日期 GitHub网站上线了,它为开源项目免费提供Git存储 --CVS及SVN都是集中式的版本控制系统, ...