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