Usage of the timer (Android learning note)

Source: Internet
Author: User

As the Android project, to use the real-time check the application launch, simply learn a timer

New a timer, then write a timertask subclass.

1  PackageComz.autoupdatefile; 2   3 ImportJava.util.Timer; 4 ImportJava.util.TimerTask; 5   6  Public classM {7      Public Static voidMain (string[] args) {8         //TODO todo.generated by Zoer9Timer timer =NewTimer (); TenTimer.schedule (NewMyTask (), 1000, 2000);  One     }   A }   -    - classMyTaskextendsTimerTask { the    - @Override -      Public voidrun () { -System.out.println ("dddd");  +    -     }   +    A}
View Code

In this way, you can start executing mytask after 1 seconds, once every two seconds.

Of course, the function of a timer can also be constructed by itself, and then by using sleep in the thread to simulate stopping for a period of time before performing an action.

In fact, look at the source of TimerTask can immediately know, TimerTask is to achieve the Runnable interface. In other words, a timer to perform an operation over a period of time is also done by a thread.

"So you can save yourself time writing threads."

Usage of the timer (Android learning note)

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.