Android Timer schedule, androidtimer

Source: Internet
Author: User

Android Timer schedule, androidtimer

Timer. schedule (new MyTask (), long time1, long timer2 );

Today, I have thoroughly understood this method that once gave me a headache. I will focus on the following:

The first parameter is the TimerTask class. In the package: import java. util. TimerTask. the user must inherit the class and implement the public void run () method, because the TimerTask class implements the Runnable interface.

The second parameter means that after you call this method, this method will inevitably call the run () method in the TimerTask class. This parameter is the difference between the two, after you call the schedule () method, you need to wait for such a long time to execute the run () method for the first time.

The third parameter indicates the time at which the run () method is called from the second time after the first call.

[Appendix:]

The most important step for technicians to integrate the internal office system with external websites is to read data from the OA system and generate the Final Static Page Based on the website template. Here we need a scheduled task for cyclic execution.

When a technician writes a scheduled task, he assumes that Timer. schedule (TimerTask task, long delay) is a repeated task. After running the program, I found that it was only run once. I always felt that the code in the task had a problem and it took a long time to debug the code without any result.

After carefully studying the java api, we found that:

Note of schedule (TimerTask task, long delay): Schedules the specified task for execution after the specified delay. The general idea is to execute the task after the delay of delay milliseconds. Repeated execution is not mentioned.

Note of schedule (TimerTask task, long delay, long period): Schedules the specified task for repeated fixed-delay execution, beginning after the specified delay. The general idea is to execute the task repeatedly after the delay of delay milliseconds, and the cycle is period milliseconds.

In this case, it is clear that schedule (TimerTask task, long delay) is executed only once, and schedule (TimerTask task, long delay, long period) is executed repeatedly. The key problem is that the programmer mistakenly thinks that schedule is repeated execution, but does not carefully study the API. On the one hand, it also lacks the English ability. The meaning cannot be quickly understood during the API browsing process.


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.