Countdown:
Main. xml:
<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<Textview Android: Id = "@ + ID/countdowntime"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/Hello"/>
</Linearlayout>
Certificate -------------------------------------------------------------------------------------------------------------------------------------------
Countdowntime. Java:
Package demo. countdowntime;
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. widget. textview;
Public class countdowntime extends activity {
Timer timer;
Int reclen = 50;
Textview rectime;
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Coutdowntime ();
}
Private void coutdowntime (){
Rectime = (textview) findviewbyid (R. Id. countdowntime );
Timer = new timer (true );
Timer. Schedule (task, 1000,100 0); // The task is executed after a delay of MS, and once a delay of MS
}
Timertask task = new timertask (){
Public void run (){
Message message = new message ();
Message. What = 1;
For (; reclen> 0; reclen --){
Handler. sendmessage (Message );
}
}
};
Final handler = new handler (){
Public void handlemessage (Message MSG ){
Switch (msg. What ){
Case 1:
Rectime. settext (string. valueof (reclen ));
Break;
}
Super. handlemessage (MSG );
}
};
}
========================================================== ==========================================================
Timer :( see: http://www.cnblogs.com/xzf158/archive/2009/09/04/1560042.html)
Main. xml:
<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<Textview Android: text = "time"
Android: Id = "@ + ID/time"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>
</Linearlayout>
Bytes ------------------------------------------------------------------------------------------------------------------------
Timer. Java:
Package demo. timer;
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. widget. textview;
Public class timer extends activity {
Timer timer;
Int reclen;
Textview rectime;
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Timer ();
}
Private void timer (){
Rectime = (textview) findviewbyid (R. Id. time );
Timer = new timer (true );
Timer. Schedule (task, 1000,100 0); // The task is executed after a delay of MS, and once a delay of MS
}
Timertask task = new timertask (){
Public void run (){
Message message = new message ();
Message. What = 1;
Handler. sendmessage (Message );
}
};
Handler handler = new handler (){
Public void handlemessage (Message MSG ){
Switch (msg. What ){
Case 1:
Reclen ++;
Rectime. settext (string. valueof (reclen ));
Break;
}
Super. handlemessage (MSG );
}
};
}