Android basic--countdowntimer class, easy to achieve the countdown function

Source: Internet
Author: User

Before this class was discovered, the handler was always used, the child thread sent messages, and the UI thread performed the countdown display work. A few days ago in a countdown to show the time to find this class, easy to use.

Look at the next source, the internal has helped us achieve the handler of the sub-threading operations



Countdowntimer This class is very simple to use, two parameters, a few lines of code, as follows:


The constructor has two parameters, the first oneMillisinfuture refers to the total time to Countdown, the unit is a long MS, the second parameter countdowninterval refers to the frequency of the countdown, is a countdown 1s or a countdown 2s

Knowing the meaning of these two parameters is very good, and the new one Countdowntimer object will generate two callback functions

public void OnTick (long millisuntilfinished) {//Todo auto-generated method stub} @Overridepublic void OnFinish () {//TODO auto-generated Method Stub}

The first method is called in the countdown time, each countdown to do what to do, you can do in this, it is generally a UI thread to do some things, such as the text changes, to achieve the effect of the countdown

The second method is the countdown is completed after the call, the countdown to complete what needs to be done can be written inside.

Of course, finally remember start () otherwise the thread will not start

All code:

public class Mainactivity extends Activity {private TextView TV; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); TV = (TextView) Findviewbyid (r.id.tv);/** Countdown 60 seconds, once 1 seconds */countdowntimer timer = new Countdowntimer (60*1000, $) {@Overridepublic void OnTick (Long millisuntilfinished) {//TODO auto-generated method Stubtv.settext ("left" +millisuntilfinished/1000+ "second");} @Overridepublic void OnFinish () {Tv.settext ("Countdown completed");}}. Start ();}}

:





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.