1. importandroid.os.Handler;
2. importandroid.os.Message;
3.
4. publicabstractclassAdvancedCountdownTimer {
5.
6. privatefinallongmCountdownInterval;
7.
8. privatelongmTotalTime;
9.
10. privatelongmRemainTime;
11.
12.
13. publicAdvancedCountdownTimer(longmillisInFuture, longcountDownInterval) {
14. mTotalTime = millisInFuture;
15. mCountdownInterval = countDownInterval;
16.
17. mRemainTime = millisInFuture;
18. }
19.
20. publicfinalvoidseek(intvalue) {
21. synchronized(AdvancedCountdownTimer.this) {
22. mRemainTime = ((100- value) * mTotalTime) / 100;
23. }
24. }
25.
26.
27. publicfinalvoidcancel() {
28. mHandler.removeMessages(MSG_RUN);
29. mHandler.removeMessages(MSG_PAUSE);
30. }
31.
32. publicfinalvoidresume() {
33. mHandler.removeMessages(MSG_PAUSE);
34. mHandler.sendMessageAtFrontOfQueue(mHandler.obtainMessage(MSG_RUN));
35. }
36.
37. publicfinalvoidpause() {
38. mHandler.removeMessages(MSG_RUN);
39. mHandler.sendMessageAtFrontOfQueue(mHandler.obtainMessage(MSG_PAUSE));
40. }
41.
42.
43. publicsynchronizedfinalAdvancedCountdownTimer start() {
44. if(mRemainTime <= 0) {
45. onFinish();
46. returnthis;
47. }
48. mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_RUN),
49. mCountdownInterval);
50. returnthis;
51. }
52.
53. publicabstractvoidonTick(longmillisUntilFinished, intpercent);
54.
55.
56. publicabstractvoidonFinish();
57.
58. privatestaticfinalintMSG_RUN = 1;
59. privatestaticfinalintMSG_PAUSE = 2;
60.
61. privateHandler mHandler = newHandler() {
62.
63. @Override
64. publicvoidhandleMessage(Message msg) {
65.
66. synchronized(AdvancedCountdownTimer.this) {
67. if(msg.what == MSG_RUN) {
68. mRemainTime = mRemainTime - mCountdownInterval;
69.
70. if(mRemainTime <= 0) {
71. onFinish();
72. } elseif(mRemainTime < mCountdownInterval) {
73. sendMessageDelayed(obtainMessage(MSG_RUN), mRemainTime);
74. } else{
75.
76. onTick(mRemainTime, newLong(100
77. * (mTotalTime - mRemainTime) / mTotalTime)
78. .intValue());
79.
80.
81. sendMessageDelayed(obtainMessage(MSG_RUN),
82. mCountdownInterval);
83. }
84. } elseif(msg.what == MSG_PAUSE) {
85.
86. }
87. }
88. }
89. };
90. }
android自带的CountdownTimer源码:
1.
2.
3. packageandroid.os;
4.
5. importandroid.util.Log;
6.
7.
8. publicabstractclassCountDownTimer {
9.
10.
11. privatefinallongmMillisInFuture;
12.
13.
14. privatefinallongmCountdownInterval;
15.
16. privatelongmStopTimeInFuture;
17.
18.
19. publicCountDownTimer(longmillisInFuture, longcountDownInterval) {
20. mMillisInFuture = millisInFuture;
21. mCountdownInterval = countDownInterval;
22. }
23.
24.
25. publicfinalvoidcancel() {
26. mHandler.removeMessages(MSG);
27. }
28.
29.
30. publicsynchronizedfinalCountDownTimer start() {
31. if(mMillisInFuture <= 0) {
32. onFinish();
33. returnthis;
34. }
35. mStopTimeInFuture = SystemClock.elapsedRealtime() + mMillisInFuture;
36. mHandler.sendMessage(mHandler.obtainMessage(MSG));
37. returnthis;
38. }
39.
40.
41.
42. publicabstractvoidonTick(longmillisUntilFinished);
43.
44.
45. publicabstractvoidonFinish();
46.
47.
48. privatestaticfinalintMSG = 1;
49.
50.
51. // handles counting down
52. privateHandler mHandler = newHandler() {
53.
54. @Override
55. publicvoidhandleMessage(Message msg) {
56.
57. synchronized(CountDownTimer.this) {
58. finallongmillisLeft = mStopTimeInFuture - SystemClock.elapsedRealtime();
59.
60. if(millisLeft <= 0) {
61. onFinish();
62. } elseif(millisLeft < mCountdownInterval) {
63. // no tick, just delay until done
64. sendMessageDelayed(obtainMessage(MSG), millisLeft);
65. } else{
66. longlastTickStart = SystemClock.elapsedRealtime();
67. onTick(millisLeft);
68.
69. // take into account user's onTick taking time to execute
70. longdelay = lastTickStart + mCountdownInterval - SystemClock.elapsedRealtime();
71.
72. // special case: user's onTick took more than interval to
73. // complete, skip to next interval
74. while(delay < 0) delay += mCountdownInterval;
75.
76. sendMessageDelayed(obtainMessage(MSG), delay);
77. }
78. }
79. }
80. };
81. }
这个是CountDownTimer的实际运用例子:
http://gundumw100.iteye.com/admin/blogs/1057989
标签:mRemainTime,millisInFuture,removeMessages,mHandler,计时器,MSG,android,CountDownTime From: https://blog.51cto.com/u_548275/6237981