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.