The usage of timer and TimerTask in Java in Android

Source: Internet
Author: User

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
  1. True indicates that the timer runs in daemon mode (Low priority, program end timer also auto-end)
  2. Java.util.Timer Timer = new Java.util.Timer (true);
  3. TimerTask task = new TimerTask () {
  4. public Void Run () {
  5. //each time the code needs to be executed is placed in this area.
  6. }
  7. };
  8. Here are a few ways to schedule a task:
  9. Time is a date type: Executed once at the specified times.
  10. Timer.schedule (task, time);
  11. Firsttime is a date type, and period is a long, which means that it is executed every period milliseconds starting at firsttime time.
  12. Timer.schedule (Task, firsttime, period);
  13. Delay is a long type: From now on, the delay is performed once in milliseconds.
  14. Timer.schedule (task, delay);
  15. Delay is long,period to long: it is executed every period milliseconds after the delay from now on.
  16. Timer.schedule (task, delay, period);


Example code:

[Java]View Plaincopy
    1. Import android.app.Activity;
    2. Import Android.os.Bundle;
    3. Import Android.os.Handler;
    4. Import Android.os.Message;
    5. Import Java.util.Timer;
    6. Import Java.util.TimerTask;
    7. Public class Timertaskactivity extends Activity {
    8. private Timer Mtimer;
    9. @Override
    10. protected void OnCreate (Bundle savedinstancestate) {
    11. super.oncreate (savedinstancestate);
    12. //init timer
    13. Mtimer = new Timer ();
    14. //Start Timer task
    15. Settimertask ();
    16. }
    17. @Override
    18. protected void OnDestroy () {
    19. Super.ondestroy ();
    20. //Cancel Timer
    21. Mtimer.cancel ();
    22. }
    23. private void Settimertask () {
    24. Mtimer.schedule (new TimerTask () {
    25. @Override
    26. public Void Run () {
    27. Message message = new Message ();
    28. Message.what = 1;
    29. Doactionhandler.sendmessage (message);
    30. }
    31. }, + /-/-1000 milliseconds after every 1000 milliseconds.
    32. }
    33. /** 
    34. * Do some action
    35. */
    36. private Handler Doactionhandler = new Handler () {
    37. @Override
    38. public void Handlemessage (Message msg) {
    39. super.handlemessage (msg);
    40. int msgId = Msg.what;
    41. switch (msgId) {
    42. case 1:
    43. //Do some action
    44. Break ;
    45. Default:
    46. Break ;
    47. }
    48. }
    49. };
    50. }

The usage of timer and TimerTask in Java in Android (GO)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.