Summary of Methods of Delayed Execution Operation in Android

  • 2021-10-16 02:36:38
  • OfStack

In the development of Android, we may have the need to delay the execution of a certain operation. For example, when we start the application, 1 starts to present a boot page, and after two or three seconds, it will automatically jump to the main interface. This is a delayed operation.

Here are several ways to implement delayed execution operations:

1. Use the sleep of threads to implement delayed operation


  new Thread() {
      @Override
      public void run() {
        super.run();
        Thread.sleep(3000);// Dormancy 3 Seconds 
        /**
         *  Action to perform 
         */
             }
    }.start();

2. Use TimerTask to implement delayed operation


 TimerTask task = new TimerTask() {
      @Override
      public void run() {
       /**
        * Action to perform 
        */
      }
    };
    Timer timer = new Timer();
    timer.schedule(task, 3000);//3 Execute in seconds TimeTask Adj. run Method 

3. Using postDelayed method of Handler to realize delayed operation


 Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
      @Override
      public void run() {
        /**
        * Action to perform 
        */
      }
    }, 3000);//3 Execute in seconds Runnable In run Method 

Summarize


Related articles: