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 ();}}
: