The countdowntimer Class records the download time. It encapsulates background thread creation and handler queue into a convenient class call.
I checked the official documentation. This class is simple and has only four methods. It involves ontick, onfinsh, cancel, and start. The first two are abstract methods, So rewrite them.
Below is a small example officially given:
1. New countdowntimer (30000,100 0 ){
2. Public void ontick (long millisuntilfinished ){
3. mtextfield. settext ("seconds remaining:" + millisuntilfinished/1000 );
4 .}
5. Public void onfinish (){
6. mtextfield. settext ("done! ");
7 .}
8.}. Start ();
The following is a modified demo.
1. Package CN. Demo;
2.
3. Import Android. App. activity;
4. Import Android. OS. Bundle;
5. Import Android. content. intent;
6. Import Android. OS. countdowntimer;
7. Import Android. widget. textview;
8. Import Android. widget. Toast;
9. Public class newactivity extends activity {
10. Private mycount MC;
11. Private textview TV;
12. @ override
13. Protected void oncreate (bundle savedinstancestate ){
14. // todo auto-generated method stub
15. Super. oncreate (savedinstancestate );
16. setcontentview (R. layout. Main );
17. TV = (textview) findviewbyid (R. Id. Show );
1. MC = new mycount (30000,100 0 );
2. MC. Start ();
3 .}
4.
5./* define an internal countdown class */
6. Class mycount extends countdowntimer {
7. Public mycount (long millisinfuture, long countdowninterval ){
8. Super (millisinfuture, countdowninterval );
9 .}
10. @ override
11. Public void onfinish (){
12. TV. settext ("finish ");
13 .}
14. @ override
15. Public void ontick (long millisuntilfinished ){
16. TV. settext ("Please wait 30 seconds (" + millisuntilfinished/1000 + ")...");
17. Toast. maketext (newactivity. This, millisuntilfinished/1000 + "", Toast. length_long). Show (); // toast has a display Time Delay
18 .}
19 .}
20 .}
The main method is to override the ontick and onfinsh methods. The code in onfinish () is what you need to do when the timer ends. The code in ontick (long m) is what you need to do when the countdown starts, the m parameter is the time until completion. In the constructor mycount (), the former is the number of Countdown times, and the latter is the interval between the second, all are in milliseconds. For example, if the countdown is 30 seconds and the interval between two seconds is 1 second, two parameters can be written as mycount ). It encapsulates background thread creation and handler queue into a convenient class call.
You can use the MC. Cancel () method to cancel the task.