Timers are typically implemented with handler and threads or timers, but Android provides a timer class Countdowntimer. Timed execution of a countdown that stops after a period of time and is notified at a fixed interval during the countdown execution (triggering the Ontick method). The creation of a background thread and the handler queue are encapsulated as a convenient class for developers to invoke.
1 /*define an inner class for a countdown*/2 classTimecountextendsCountdowntimer {3 PublicTimecount (LongMillisinfuture,Longcountdowninterval) {4 Super(Millisinfuture, Countdowninterval);//The parameter is the total length of time, and the timing interval5 }6 7 @Override8 Public voidOnFinish () {//trigger When timing is complete9Btnresend.settext ("Resend Verification Code");TenBtnresend.setclickable (true); One } A - @Override - Public voidOnTick (Longmillisuntilfinished) {//Timing Process Display theBtnresend.setclickable (false); -Btnresend.settext (millisuntilfinished/1000 + "SEC"); - } -}
The main thing is to rewrite the two methods of OnTick and Onfinsh, the code in OnFinish () is what to do when the timer ends, and the code in OnTick (Long m) is the thing to do when you start the countdown, and the parameter m is the time until completion.
The timer creation example is as follows:
1 New Timecount (12000, 1000);
1 // Resend Verification Code 2 btnresend.setonclicklistener (new Onclicklistener () {34 @ Override5public void OnClick (View v) {6 Time.start (); 7 }8 });
The two parameters in the construction method are the number of times that are inverted, which is the interval between the middle of the inverted meter and the time in milliseconds.
Daily Summary-Android Timer class Countdowntimer