CountDownTimer用法
来源:互联网 发布:mac dock栏图标隐藏 编辑:程序博客网 时间:2024/06/05 08:55
Section1
new CountDownTimer(10000, 1000) { @Override public void onTick(long millisUntilFinished) { } @Override public void onFinish() { }}.start();
看源码解释
/** * @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;}第一个参数代表倒计时多少毫秒,第二个参数代表倒计时的间隔。Section2
第一个问题,是否延迟final long starttime = System.currentTimeMillis();new CountDownTimer(10000, 1000) { @Override public void onTick(long millisUntilFinished) { long tickTime = System.currentTimeMillis(); double betweenTime = tickTime-starttime; betweenTime = (betweenTime/1000d); Toast.makeText(MainActivity.this,betweenTime+"",Toast.LENGTH_SHORT).show(); } @Override public void onFinish() { }}.start();从Toast提示可以看出,代码逻辑上并不延迟,只不过代码运行需要点时间。Section3
那么,如果不延迟的话,理论上来说final long starttime = System.currentTimeMillis();new CountDownTimer(5000, 1000) { @Override public void onTick(long millisUntilFinished) { long tickTime = System.currentTimeMillis(); double betweenTime = tickTime-starttime; betweenTime = (betweenTime/1000d); Toast.makeText(MainActivity.this,betweenTime+"",Toast.LENGTH_SHORT).show(); } @Override public void onFinish() { }}.start();应该一共弹 0,1,2,34,5事实上只弹了 0,1,2,3
Section4
4和5 哪去了?
5应该是在onFinish里面
final long starttime = System.currentTimeMillis();new CountDownTimer(5000, 1000) { @Override public void onTick(long millisUntilFinished) { long tickTime = System.currentTimeMillis(); double betweenTime = tickTime-starttime; betweenTime = (betweenTime/1000d); Toast.makeText(MainActivity.this,betweenTime+"",Toast.LENGTH_SHORT).show(); } @Override public void onFinish() { long tickTime = System.currentTimeMillis(); double betweenTime = tickTime-starttime; betweenTime = (betweenTime/1000d); Toast.makeText(MainActivity.this,betweenTime+"",Toast.LENGTH_SHORT).show(); }}.start();这次是 0,1,2,3,5
Section5
还是缺一个,
看源码
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); }}else if (millisLeft < mCountdownInterval)这里小于时间间隔就不tick了好吧,我们改进一下final long starttime = System.currentTimeMillis();new CountDownTimer(5100, 1000) { @Override public void onTick(long millisUntilFinished) { long tickTime = System.currentTimeMillis(); double betweenTime = tickTime-starttime; betweenTime = (betweenTime/1000d); Toast.makeText(MainActivity.this,betweenTime+"",Toast.LENGTH_SHORT).show(); } @Override public void onFinish() { long tickTime = System.currentTimeMillis(); double betweenTime = tickTime-starttime; betweenTime = (betweenTime/1000d); Toast.makeText(MainActivity.this,betweenTime+"",Toast.LENGTH_SHORT).show(); }}.start();这次终于0,1,2,3,4,5 都有了。。
这里关键点是把5000改为5100。把损耗时间算上。
阅读全文
0 0
- CountDownTimer用法
- CountDownTimer用法
- CountDownTimer用法详解
- CountDownTimer用法详解
- CountDownTimer
- CountDownTimer
- CountdownTimer
- CountDownTimer
- CountDownTimer
- CountDownTimer
- CountDownTimer
- Android倒计时定时器CountDownTimer的用法
- Android中CountDownTimer倒计时的用法
- android里CountDownTimer类的用法
- android 关于倒计时CountDownTimer的用法和实例
- CountDownTimer倒计时
- Android CountDownTimer
- CountDownTimer倒计时
- 机器学习(4)分类之集成方法
- Android ConstraintLayout 使用详解
- Matplotlib库学习(一)plt.plot
- 【Windows 10 应用开发】细说文本资源文件(resw)
- python计算RGB图片H通道
- CountDownTimer用法
- c++primer 6.3.3节练习(返回数组指针的4个方法)
- C++:STL标准入门汇总
- The hierarchy of the type LogBeforeAdvice is inconsistent
- PuTTY + Xming 实现图形安装 Oracle
- SSM框架下分页的实现(封装page.java和List<?>)
- 顺序表各种基本运算的算法
- 设计模式 5/23 原型模式
- cnn、rnn实现中文文本分类(基于tensorflow)