Android countdowntimer implements countdown timer

Introduction to use

Some countdown related scenarios are often encountered in development, such as the button to send verification code. After clicking send, the countdown time will be displayed. After the countdown, the button can be refreshed and allowed to click again. In order not to block the operation of the software, but also to refresh the interface in real time, we usually use technologies such as handler or asynctask to write our own logic implementation. In fact, a set of countdowntimer has been encapsulated in Android to realize this functional requirement.

CountDownTimer(long millisInFuture,long countDownInterval)

The two parameters of countdowntimer represent the total countdown time millisinfuture and the interval countdowninterval respectively.

The specific calls are as follows:

The above call example indicates that the ontick method will be executed every 1 second for a total of 60 seconds. The parameter millisuntilfinished indicates the number of milliseconds remaining in the countdown. Finally, the onfinish method will be executed after the countdown is completed.

Implementation principle

The following is the source code of countdowntimer. There is very little code, which is easy to understand. As can be seen from the source code, countdowntimer also uses the message processing mechanism of handler to achieve the effect. After initializing and setting the start and end times, send messages to the main thread through the handler at certain intervals, and then call back the method in message processing. Making good use of the official encapsulated tools can avoid us building wheels repeatedly. Of course, it's better to understand the principle of wheels!

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>