android development tutorials are separated by executing the of android timer

  • 2020-05-27 07:07:59
  • OfStack

The following is an operation performed every 1 period until the timing operation is turned off:


final Handler handler = new Handler();
     Runnable runnable = new Runnable(){
         @Override
         public void run() {
             // TODO Auto-generated method stub
             //  Add the executed code here 
             handler.postDelayed(this, 50);// 50 Is the delay time 
         } 
     }; 
     handler.postDelayed(runnable, 50);//  Turn on the timer and perform the operation 
     handler.removeCallbacks(this);//  Turn off timer processing 

The following is an operation that is performed once after a period of time, and when it is finished, it will not be performed again


final Handler handler = new Handler();
        runCount = 0;//  Global variable used to determine whether or not the 1 Time to perform 
        Runnable runnable = new Runnable(){
    @Override
    public void run() {
     // TODO Auto-generated method stub
     if(runCount == 1){//  The first 1 The second execution closes the timed execution operation 
      //  Add the executed code here 
      handler.removeCallbacks(this);
     }
     handler.postDelayed(this, 50);
     runCount++;
    }

        };
        handler.postDelayed(runnable, 50);//  Turn on the timer and perform the operation 


Related articles: