Transferred from: http://blog.csdn.net/zuolongsnail/article/details/8168689
In development we sometimes have the need to perform a task at regular intervals. For example, the controls on the UI need to change over time, and we can use the tool class for the timers that Java provides us, namely timer and TimerTask.
The timer is an ordinary class, there are several important methods, and TimerTask is an abstract class, which has an abstract method run (), similar to the run () method in the thread, we use the timer to create an object, The schedule method of this object is then used to complete the operation of this interval.
The schedule method has three parameters the first parameter is the object of the TimerTask type, we implement the TimerTask run () method is one of the tasks to perform the cycle, the second parameter has two types, the first is a long type, which indicates how long it takes to start execution, The other is a date type, which indicates that execution begins after that time, and the third parameter is the period of execution, which is a long type.
The schedule method also has a two-parameter execution overload, the first argument is still TimerTask, the second is represented as a long, and is executed once, for date, after a certain time.
The timer is a thread, using the schedule method to complete the dispatch of the TimerTask, multiple timertask can share a timer, that is, the timer object calls a schedule method is to create a thread, And once schedule is called, the TimerTask is unrestricted, and the timer's cancel () is used to stop the operation. Of course, after the same timer executes a cancel () method, all timer threads are terminated.
Usage:
[Java]View Plaincopy
- True indicates that the timer runs in daemon mode (Low priority, program end timer also auto-end)
- Java.util.Timer Timer = new Java.util.Timer (true);
- TimerTask task = new TimerTask () {
- public Void Run () {
- //each time the code needs to be executed is placed in this area.
- }
- };
- Here are a few ways to schedule a task:
- Time is a date type: Executed once at the specified times.
- Timer.schedule (task, time);
- Firsttime is a date type, and period is a long, which means that it is executed every period milliseconds starting at firsttime time.
- Timer.schedule (Task, firsttime, period);
- Delay is a long type: From now on, the delay is performed once in milliseconds.
- Timer.schedule (task, delay);
- Delay is long,period to long: it is executed every period milliseconds after the delay from now on.
- Timer.schedule (task, delay, period);
Example code:
[Java]View Plaincopy
- Import android.app.Activity;
- Import Android.os.Bundle;
- Import Android.os.Handler;
- Import Android.os.Message;
- Import Java.util.Timer;
- Import Java.util.TimerTask;
- Public class Timertaskactivity extends Activity {
- private Timer Mtimer;
- @Override
- protected void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- //init timer
- Mtimer = new Timer ();
- //Start Timer task
- Settimertask ();
- }
- @Override
- protected void OnDestroy () {
- Super.ondestroy ();
- //Cancel Timer
- Mtimer.cancel ();
- }
- private void Settimertask () {
- Mtimer.schedule (new TimerTask () {
- @Override
- public Void Run () {
- Message message = new Message ();
- Message.what = 1;
- Doactionhandler.sendmessage (message);
- }
- }, + /-/-1000 milliseconds after every 1000 milliseconds.
- }
- /**
- * Do some action
- */
- private Handler Doactionhandler = new Handler () {
- @Override
- public void Handlemessage (Message msg) {
- super.handlemessage (msg);
- int msgId = Msg.what;
- switch (msgId) {
- case 1:
- //Do some action
- Break ;
- Default:
- Break ;
- }
- }
- };
- }
The usage of timer and TimerTask in Java in Android (GO)