Wait for 5 seconds

前端 未结 8 1875
借酒劲吻你
借酒劲吻你 2021-02-01 04:00

I want to wait 5 seconds before starting another public void method. The thread sleep was not working for me. If there is a way of wait() without using Threads I wo

相关标签:
8条回答
  • 2021-02-01 04:28

    just add one-liner with lambda

    (new Handler()).postDelayed(this::yourMethod, 5000);
    

    edit for clarification: yourMethod refers to the method which you want to execute after 5000 milliseconds.

    0 讨论(0)
  • 2021-02-01 04:32

    you can use java handlers to achieve your task:

    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        public void run() {
         // Actions to do after 5 seconds
        }
    }, 5000);
    

    for more information read the following url:

    https://developer.android.com/reference/android/os/Handler.html

    0 讨论(0)
  • 2021-02-01 04:34

    what I prefer is

    (new Handler()).postDelayed(this::here is your method,2000);
    
    0 讨论(0)
  • 2021-02-01 04:39

    See if this works for you. Be sure to import the android.os.Handler

          Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    public void run() {
                        // yourMethod();
                    }
                }, 5000);   //5 seconds
    
    0 讨论(0)
  • 2021-02-01 04:41

    For import use : import android.os.Handler;

     new Handler().postDelayed(new Runnable() {
                public void run() {
                    // yourMethod();
                }
            }, 5000);   //5 seconds
    
    0 讨论(0)
  • 2021-02-01 04:42

    I use the following code (the same as you see before) for my android app, i need to wait some threads before start my new method. It works fine.

     Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                public void run() {
                    // yourMethod();
                }
            }, 5000);   //5 seconds
    
    0 讨论(0)
提交回复
热议问题