Go directly to the code and explain the comments. An example of a rocket launch countdown
Main. xml
[HTML] <? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: Orientation = "vertical">
<Button
Android: Id = "@ + ID/button"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "Start countdown"/>
<Textview
Android: Id = "@ + ID/textview"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>
</Linearlayout>
<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: Orientation = "vertical">
<Button
Android: Id = "@ + ID/button"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "Start countdown"/>
<Textview
Android: Id = "@ + ID/textview"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>
</Linearlayout>
Timerdemoactivity. Java
[Java] package com. tianjf;
Import java. util. timer;
Import java. util. timertask;
Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. OS. Handler;
Import Android. OS. message;
Import Android. util. log;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. widget. Button;
Import Android. widget. textview;
Public class timerdemoactivity extends activity implements onclicklistener {
Private button;
Private textview;
Private timer;
// Define Handler
Handler handler = new handler (){
@ Override
Public void handlemessage (Message MSG ){
Super. handlemessage (MSG );
Log. D ("debug", "thread where the handlemessage method is located :"
+ Thread. currentthread (). getname ());
// Handler processes the message
If (msg. What> 0 ){
Textview. settext (msg. What + "");
} Else {
Textview. settext ("ignition! ");
// End the timer Timer
Timer. Cancel ();
}
}
};
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Button = (button) findviewbyid (R. Id. Button );
Textview = (textview) findviewbyid (R. Id. textview );
Log. D ("debug", "thread where the oncreate method is located :"
+ Thread. currentthread (). getname ());
Button. setonclicklistener (this );
}
@ Override
Public void onclick (view v ){
Switch (V. GETID ()){
Case R. Id. Button:
// Create a timer when the button is pressed
Timer = new timer ();
// Create a timertask
// Timertask is an abstract class that implements the runnable interface. Therefore, timertask is a subthread.
Timertask = new timertask (){
// Countdown 10 seconds
Int I = 10;
@ Override
Public void run (){
Log. D ("debug", "thread where the run method is located :"
+ Thread. currentthread (). getname ());
// Define a message to pass
Message MSG = new message ();
MSG. What = I --;
Handler. sendmessage (MSG );
}
};
// Define a scheduled task and perform the following tasks based on different parameters:
// 1. Schedule (timertask task, date when) Schedule> execute a task at a fixed time
// 2. Schedule (timertask task, date when, long period) Progress> repeat a task at a fixed time, with controllable repetition Interval
// 3. Schedule (timertask task, long delay) Delay> after how long the task will be executed
// 4. Schedule (timertask task, long delay, long period) interval> repeat a task after the delay, with controllable repetition Interval
Timer. Schedule (timertask, 3000,100 0); // The countdown starts after 3 seconds. The countdown interval is 1 second.
Break;
Default:
Break;
}
}
}
From the column of zookeeper