对于一个Java开发者来说,想到定时器(计时器,倒计时)的应用 一般都会想到使用 java.util.Timer 和 java.util.TimerTask , 在Android中使用这2个类也可以实现计时的功能 但是使用起来还是有点麻烦的, 特别是在UI界面需要更新的时候, 例如 实现一个倒计时的界面, 在一个TextView中显示当前剩余的时间, 如果在TimerTask中计时,则无法更新TextView显示的剩余时间(在非UI线程中 不能访问UI组件),可以通过runOnUiThread函数来实现 但是多少有点烦琐.

在Android 实现定时比较推荐的方式还是使用 android.os.Handler 中的 postXXX 和sendXXX 等方法. 细心的开发者可能已经注意到Android提供了一个倒计时的助手类 android.os.CountDownTimer 来方便实现倒计时的功能. 她就是通过handler的 sendMessageDelayed 来实现的. 使用该类来显示一个倒计时的TextView是很方便的:


01. TextView mTv; 02. @Override 03. protected void onCreate(Bundle savedInstanceState) { 04. super .onCreate(savedInstanceState); 05. setContentView(R.layout.main); 06. mTv = (TextView) findViewById(R.id.tv); 07. mTv.setText( "test" ); 08. new CountDownTimer( 30000 , 100 ) { 09. 10. @Override 11. public void onTick( long millisUntilFinished) { 12. mTv.setText( "seconds remaining: " + 13. millisUntilFinished / 1000 + " S " +(millisUntilFinished % 1000 )/ 100 ); 14. } 15. 16. @Override 17. public void onFinish() { 18. mTv.setText( "Done!" ); 19. } 20. }.start(); 21. }

CountDownTimer的源代码如下:

01. public abstract class CountDownTimer { 02. 03. /** 04. * Millis since epoch when alarm should stop. 05. */ 06. private final long mMillisInFuture; 07. 08. /** 09. * The interval in millis that the user receives callbacks 10. */ 11. private final long mCountdownInterval; 12. 13. private long mStopTimeInFuture; 14. 15. /** 16. * @param millisInFuture The number of millis in the future from the call 17. * to {@link #start()} until the countdown is done and {@link #onFinish()} 18. * is called. 19. * @param countDownInterval The interval along the way to receive 20. * {@link #onTick(long)} callbacks. 21. */ 22. public CountDownTimer( long millisInFuture, long countDownInterval) { 23. mMillisInFuture = millisInFuture; 24. mCountdownInterval = countDownInterval; 25. } 26. 27. /** 28. * Cancel the countdown. 29. */ 30. public final void cancel() { 31. mHandler.removeMessages(MSG); 32. } 33. 34. /** 35. * Start the countdown. 36. */ 37. public synchronized final CountDownTimer start() { 38. if (mMillisInFuture <= 0 ) { 39. onFinish(); 40. return this ; 41. } 42. mStopTimeInFuture = SystemClock.elapsedRealtime() + mMillisInFuture; 43. mHandler.sendMessage(mHandler.obtainMessage(MSG)); 44. return this ; 45. } 46. 47. 48. /** 49. * Callback fired on regular interval. 50. * @param millisUntilFinished The amount of time until finished. 51. */ 52. public abstract void onTick( long millisUntilFinished); 53. 54. /** 55. * Callback fired when the time is up. 56. */ 57. public abstract void onFinish(); 58. 59. 60. private static final int MSG = 1 ; 61. 62. 63. // handles counting down 64. private Handler mHandler = new Handler() { 65. 66. @Override 67. public void handleMessage(Message msg) { 68. 69. synchronized (CountDownTimer. this ) { 70. final long millisLeft = mStopTimeInFuture - SystemClock.elapsedRealtime(); 71. 72. if (millisLeft <= 0 ) { 73. onFinish(); 74. } else if (millisLeft < mCountdownInterval) { 75. // no tick, just delay until done 76. sendMessageDelayed(obtainMessage(MSG), millisLeft); 77. } else { 78. long lastTickStart = SystemClock.elapsedRealtime(); 79. onTick(millisLeft); 80. 81. // take into account user's onTick taking time to execute 82. long delay = lastTickStart + mCountdownInterval - SystemClock.elapsedRealtime(); 83. 84. // special case: user's onTick took more than interval to 85. // complete, skip to next interval 86. while (delay < 0 ) delay += mCountdownInterval; 87. 88. sendMessageDelayed(obtainMessage(MSG), delay); 89. } 90. } 91. } 92. }; }
here:http://www.chengyunfeng.com/2010/11/android-timer

更多相关文章

  1. Qt on Android:图文详解QT开发Andriod入门,Hello World全过程
  2. 在GitHub平台上,究竟有哪些Android开源项目
  3. Android+J2ee系统集成开发
  4. Android(安卓)Service的使用方法 音乐播放器实例
  5. Android的界面设计规范-5
  6. Android(安卓)View 如何去自定义View
  7. 仿微信UI界面WeChatUI
  8. android 中 FLAG_SHOW_WHEN_LOCKED 的用法及解释
  9. 深入观察Android(安卓)Wear,Google 智能手表平台

随机推荐

  1. 全球AI芯片技术选型
  2. ant_时提示前言不允许有内容问题
  3. JavaScript 基本数据类型
  4. 深度:RISC-V指令集架构和生态
  5. web前端入门到实战:css 中的背景图片小技
  6. 免费下载SVCI 2020 V12.0:升级了梅赛德斯-
  7. 为什么越来越多的数据分析师在用思迈特软
  8. 数字化转型中的数据安全问题探讨
  9. ITSS运行维护标准认证的好处
  10. 摩杜云:针对网络安全,打造云上安全堡垒