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.