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, we can use Java to provide us with the Timer tool class, that is timer and timertask.
A timer is an ordinary class with several important methods, and TimerTask is an abstract class with an abstract method run (), similar to the run () method in a thread, where we use a timer to create an object of his, 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, and our implementation of the TimerTask run () method is a task to be performed periodically;
The second parameter has two types, the first is a long type, indicating how long it starts and the other is the date type, which indicates that execution begins after that time;
The third parameter is the period of execution, which is a long type.
The schedule method also has an overloaded execution of two parameters, the first argument is still timertask, and the second is a long representation of how long it takes to execute once, and for date it represents a time after execution.
A timer is a thread that uses the schedule method to schedule the TimerTask, and multiple timertask can share a timer, which means that the timer object calls once schedule method creates a thread, And the TimerTask is unrestricted after the call is schedule, using the timer's cancel () to stop the operation. Of course, the same timer. Once the Cancel () method is executed, all timer threads are terminated.
Usage:
True indicates that the timer runs in daemon (Low priority, end timer automatically ends)
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 here.)
}
};
Here are several ways to schedule a task:
//time is a date type: Once at a specified time.
Timer.schedule (task, time);
Firsttime is a date type and period is long, which is executed every period millisecond from firsttime time.
timer.schedule (Task, firsttime, period);
Delay is long: delay milliseconds from now to execute once.
timer.schedule (task, delay);
Delay is long,period long: Once delay milliseconds from now on, it is performed every period milliseconds.
timer.schedule (task, delay, period);
Sample code:
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 vo
ID run () {Message message = new Message ();
Message.what = 1;
Doactionhandler.sendmessage (message);
}, 1000, 1000/* that after 1000 milliseconds, it is performed every 1000 milliseconds.
}/** * do some action* Private Handler Doactionhandler = new Handler () {@Override public void Handlemessage (msg)
{super.handlemessage (msg);
int msgId = Msg.what;
Switch (msgId) {Case 1://Does some action break;
Default:break;
}
}
}; }