Android implements the countdown function based on countdowntimer

This paper describes the method of countdown function based on countdowntimer in Android programming. Share with you for your reference, as follows:

When visiting the forum, I saw a netizen ask a question and talked about the countdowntimer class. You can see from the name and record the download time. Encapsulate the creation of background thread and handler queue into a convenient class call.

After checking the official documentation, this class is extremely simple. There are only four methods, all of which involve ontick, onfish, cancel and start. The first two are abstract methods, so we need to rewrite them.

Here is a small example given by the official:

Directly use the netizen's code, I changed it a little, a simple little demo.

It mainly rewrites ontick and onfinish methods. The code in onfinish () is what to do when the timer ends; The code in ontick (long m) is what you need to do at the beginning of the countdown. Parameter m is the time until completion. Of the two parameters in the construction method mycount(), the former is the countdown time and the latter is the interval between countdown ontick event responses, both in milliseconds. For example, to count down 30 seconds, the intermediate interval per second is 1 second. The two parameters can be MYCOUNT (300001000). The creation of background thread and the encapsulation of handler queue become a convenient class call.

When you want to cancel, just use the MC. Cancel () method.

I hope this article will help you in Android programming.

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
分享
二维码
< <上一篇
下一篇>>