Android--The countdown implementation

Source: Internet
Author: User

Countdowntimer

Countdowntimer This class, the realization of the countdown function. The creation of a background thread and the handler queue are encapsulated into a convenient class call.

This class is relatively simple, with only four methods: Ontick,onfinsh, Cancel, and start. The first two are abstract methods, so rewrite them.

Here is a small example of the official offer:

 new  Countdowntimer (30000, 1000 Span style= "color: #0000ff;" >public  void  OnTick (long   millisuntilfinished) {Mtextfield.settext ( "seconds remaining:" + Millisuntilfinished/1000);  public  void   OnFinish () {Mtextfield.settext ( "done!")      ); }}.start ();  

Code

 PackageCom.yydcdut.daojishi;ImportAndroid.os.Bundle;ImportAndroid.os.CountDownTimer;Importandroid.app.Activity;ImportAndroid.view.Menu;ImportAndroid.widget.TextView;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {PrivateMyCount MC; PrivateTextView TV; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); TV=(TextView) Findviewbyid (r.id.show); MC=NewMyCount (30000, 1000);    Mc.start (); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; }        /*define an inner class for a countdown*/      classMyCountextendsCountdowntimer { PublicMyCount (LongMillisinfuture,Longcountdowninterval) {                 Super(Millisinfuture, countdowninterval); } @Override Public voidonfinish () {Tv.settext ("Finish"); } @Override Public voidOnTick (Longmillisuntilfinished) {Tv.settext ("Please wait 30 seconds (" + millisuntilfinished/1000 + ") ..."); Toast.maketext (mainactivity. This, millisuntilfinished/1000 + "", Toast.length_long). Show ();//Toast has display time delay        }        }     }

The main thing is to rewrite the two methods of OnTick and Onfinsh, thecode 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 until the time of completion, The two parameters in the constructor method, Mycount (), are the number of times in the inverted count, which is the interval between the second and the middle of the inverted meter, in milliseconds . For example, to Countdown 30 seconds, the intermediate interval per second is 1 seconds, two parameters can write Mycount (30000,1000). The creation of a background thread and the handler queue are encapsulated as a convenient class call.

You can use the Mc.cancel () method when you want to cancel.

I'm the dividing line of the king of the Land Tiger.

Source code: HTTP://PAN.BAIDU.COM/S/1DD1QX01

Countdown. zip

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.