Countdowntimer Analysis of Countdown control in Android1Sample CodeNewCountdowntimer (10000, 1000) { Public voidOnTick (Longmillisuntilfinished) {logutil.i (TAG,"Seconds remaining:" + millisuntilfinished/1000); } Public voidonfinish () {logutil.i (TAG,"Done!"); }}.start ();2API parsing Countdowntimer (LongMillisinfuture,Longcountdowninterval) Millisinfuture: You want to countdown total time, Unit Ms.countdowninterval: you want to countdown time interval, unit Ms. Public Final voidCancel ()//Cancel the current task Public Abstract voidOnFinish ()//callback when the current task is completed Public Abstract voidOnTick (Longmillisuntilfinished)//callback for the current task every time the countdown interval is completed Public FinalCountdowntimer Start ()//start the current task3Source Analysis Public Abstract classCountdowntimer {Private Final LongMmillisinfuture;//total time for Countdown Private Final LongMcountdowninterval;//Countdown time interval Private Longmstoptimeinfuture; Private Booleanmcancelled =false;//whether to cancel the timing task//Construction Method PublicCountdowntimer (LongMillisinfuture,Longcountdowninterval) {Mmillisinfuture=millisinfuture; Mcountdowninterval=Countdowninterval; } //Cancel Countdown Public synchronized Final voidCancel () {mcancelled=true; Mhandler.removemessages (MSG); } //start the countdown . Public synchronized FinalCountdowntimer Start () {mcancelled=false; if(mmillisinfuture <= 0) {onfinish (); return This; } mstoptimeinfuture= Systemclock.elapsedrealtime () +mmillisinfuture; Mhandler.sendmessage (Mhandler.obtainmessage (MSG)); return This; } //methods of periodic callbacks Public Abstract voidOnTick (Longmillisuntilfinished); //callback method for timing end Public Abstract voidonfinish (); Private Static Final intMSG = 1; //The Countdowntimer uses the handler mechanism, which sends a message to the looper of the main thread by sendmessagedelayed delay.//You then judge the time remaining after you receive it and issue a callback, and then send the message again. //It's good to cancel the countdown and remove the task from the MessageQueue. PrivateHandler Mhandler =NewHandler () {@Override Public voidhandlemessage (Message msg) {synchronized(Countdowntimer. This) { if(mcancelled) {return; } Final LongMillisleft = Mstoptimeinfuture-systemclock.elapsedrealtime ();//Time Remaining if(Millisleft <= 0) {onfinish (); } Else if(Millisleft <mcountdowninterval) {sendmessagedelayed (Obtainmessage (MSG), millisleft); } Else { LongLasttickstart =Systemclock.elapsedrealtime (); OnTick (Millisleft); LongDelay = Lasttickstart + Mcountdowninterval-Systemclock.elapsedrealtime (); while(Delay < 0) Delay + =Mcountdowninterval; Sendmessagedelayed (Obtainmessage (MSG), delay); } } } };}4Some notes1elapsedrealtime currenttimemillis () Difference System.currenttimemillis () Gets the system time, which is a value calculated starting from January 1, 1970 ; Android.os.SystemClock.elapsedRealtime () Gets the time value that has passed since the boot of the device. System.currenttimemillis () to get the current date is meaningful, such as the current XXXX year xx months xx xx minutes xx seconds xxx milliseconds, this value can be changed in the system settings; Systemclock.elapsedrealtime () It is useful to calculate how long a time has gone through, such as how long a call has gone through, and this value is not related to system setup. But the Android source code to calculate the talk time is used System.currenttimemillis (), this generally is no problem. But if someone modifies the system time after the device has been set up, the time value after the call is not right, the date is adjusted for a few years, and the talk time is displayed for several years2sendmessagedelayed ()BooleanSendemptymessagedelayed (intWhat,Longdelaymillis) Send a message that contains only what is delaymillis after a certain time elapses
Countdown control Countdowntimer analysis in Android