Android timed execution for a period of time

Source: Internet
Author: User

In some cases, you need to perform an operation at intervals or perform an operation at intervals of time:

The following describes how to perform an operation at intervals until the scheduled operation is disabled:

 
Final handler = new handler (); runnable = new runnable () {@ override public void run () {// todo auto-generated method stub // Add the executionCodeHandler. postdelayed (this, 50); // 50 is the delay duration}; handler. postdelayed (runnable, 50); // enable the timer and execute the handler operation. removecallbacks (this); // disable timer Processing

The following is an operation executed once after a period of time.

Final handler = new handler (); runcount = 0; // global variable, used to determine whether the first runnable = new runnable () {@ overridepublic void run () {// todo auto-generated method stubif (runcount = 1) {// when the first execution is performed, disable the scheduled execution operation. // Add the executed code handler here. removecallbacks (this);} handler. postdelayed (this, 50); runcount ++ ;}}; handler. postdelayed (runnable, 50); // enable the timer and perform the operation

I have passed the test. If there is a better way, I hope I can communicate with each other.

 
 
 
 
 
 

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.