Android is based on Countdowntimer to implement the countdown function _android

Source: Internet
Author: User
Tags time interval

The example in this article describes the way that Android programming is based on Countdowntimer to implement the Countdown function. Share to everyone for your reference, specific as follows:

In the forum, see a netizen questions, said Countdowntimer This class, from the name above everyone can see, record download time. Encapsulates the creation and handler queues of background threads into a convenient class invocation.

Check out the official document, this class and its simplicity, with only four methods, all of which involve Ontick,onfinsh, Cancel, and start. The first two of these are abstract methods, so rewrite them.

The following is a small example of an official offer:

New Countdowntimer (30000, 1000) {public
  void Ontick (Long millisuntilfinished) {
    mtextfield.settext ("Seconds Remaining: "+ millisuntilfinished/1000);
  }
  public void OnFinish () {
    mtextfield.settext ("done!");
  }
. Start ();

Direct use of the user's code, their own slightly changed a little, a simple demo.

Package Cn.demo;
Import android.app.Activity;
Import Android.os.Bundle;
Import android.content.Intent;
Import Android.os.CountDownTimer;
Import Android.widget.TextView;
Import Android.widget.Toast;
  public class Newactivity extends activity {private Mycount MC;
  Private TextView TV; @Override protected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method stub super.oncreate
    (savedinstancestate);
    Setcontentview (R.layout.main);
    TV = (TextView) Findviewbyid (r.id.show);
    MC = new Mycount (30000, 1000);
  Mc.start (); }//end Func/* Defines a countdown internal class */class Mycount extends Countdowntimer {public mycount (long millisinfuture, long countd
    Owninterval) {super (millisinfuture, countdowninterval);
    @Override public void OnFinish () {Tv.settext ("Finish");  @Override public void Ontick (long millisuntilfinished) {Tv.settext ("Please wait 30 seconds" ("+ millisuntilfinished/1000
      + ")..."); Toast.maketext (newactivity.tHe, millisuntilfinished/1000 + "", Toast.length_long). Show ();//toast has display time delay}}

 

The main thing is to rewrite the two methods of Ontick and Onfinsh, the code in OnFinish () is what to do at the end of the timer; the code in Ontick (Long m) is what you do at the beginning of the countdown, the parameter m is until the completion time, the construction method Mycount () In the two parameters in, the former is the number of elapsed times, which is the time interval for the Countdown Ontick event response, in milliseconds. For example, to Countdown to 30 seconds, the intermediate interval is 1 seconds per second, and two parameters can be mycount (30000,1000). The creation of background threads and the handler queue encapsulation became a convenient class invocation.

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

I hope this article will help you with the Android program.

Related Article

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.