In Android, the UI is updated regularly.Java. util. Timer,Java. util. TimerTask, Android. OS.Handler combination. In fact, Handler itself provides the timed function.
See the android. OS. Handler documentation.
There are two main uses for a Handler:(1) to schedule messages and runnables to be executed as some point in the future;And (2) to enqueue an action to be passed med on a different thread than your own.
Scheduling messages is accomplished with the post (Runnable), postAtTime (Runnable, long), postDelayed (Runnable, long), sendEmptyMessage (int), sendMessage (Message), sendMessageAtTime (Message, long), and sendMessageDelayed (Message, long) methods. the post versions allow you to enqueue Runnable objects to be called by the message queue when they are encoded Ed; the sendMessage versions allow you to enqueue a Message object containing a bundle of data that will be processed by the Handler's handleMessage (Message) method (requiring that you implement a subclass of Handler)
When posting or sending to a Handler, you can either allow the item to be processed as soon as the message queue is ready to do so, or specify a delay before it gets processed or absolute time for it to be processed.The latter two allow you to implement timeouts, ticks, and other timing-based behavior.
The following is a simple counter program that increments the counter every second.
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/counter" android: layout_width = "fill_parent"
Android: layout_height = "wrap_content" android: text = "Count: 0"/>
<LinearLayout android: orientation = "horizontal"
Android: layout_width = "fill_parent" android: layout_height = "wrap_content">
<Button android: text = "start" android: id = "@ + id/Button01"
Android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: layout_weight = "1.0"> </Button>
<Button android: text = "stop" android: id = "@ + id/Button02"
Android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: layout_weight = "1.0" android: enabled = "false"> </Button>
<Button android: text = "reset" android: id = "@ + id/Button03"
Android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: layout_weight = "1.0"> </Button>
</LinearLayout>
</LinearLayout>
Java code:
Import android. app. Activity;
Import android. OS. Bundle;
Import android. OS. Handler;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. TextView;
Public class TestTimer extends Activity {
Private Button btnStart;
Private Button btnStop;
Private Button btnReset;
Private TextView tvCounter;
Private long count = 0;
Private boolean run = false;
Private Handler handler = new Handler ();
Private Runnable task = new Runnable (){
Public void run (){
// TODO Auto-generated method stub
If (run ){
Handler. postDelayed (this, 1000 );
Count ++;
}
TvCounter. setText ("Count:" + count );
}
};
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
BtnStart = (Button) findViewById (R. id. Button01 );
BtnStop = (Button) findViewById (R. id. Button02 );
BtnReset = (Button) findViewById (R. id. Button03 );
TvCounter = (TextView) findViewById (R. id. counter );
BtnStart. setOnClickListener (new OnClickListener (){
Public void onClick (View v ){
// TODO Auto-generated method stub
Run = true;
UpdateButton ();
Handler. postDelayed (task, 1000 );
}
});
BtnStop. setOnClickListener (new OnClickListener (){
Public void onClick (View v ){
// TODO Auto-generated method stub
Run = false;
UpdateButton ();
Handler. post (task );
}
});
BtnReset. setOnClickListener (new OnClickListener (){
Public void onClick (View v ){
// TODO Auto-generated method stub
Count = 0;
Run = false;
UpdateButton ();
Handler. post (task );
}
});
}
Private void updateButton (){
BtnStart. setEnabled (! Run );
BtnStop. setEnabled (run );
}
}