Android sleep() without blocking UI

前端 未结 4 1439
无人及你
无人及你 2020-12-14 07:44

For my new Android application I need a function, that timeout my application for 3 Seconds. I tried the function \"sleep()\" like this:

seekBar1.setProgress         


        
相关标签:
4条回答
  • 2020-12-14 08:21

    You can use postDelayed() method like this:

    handler=new Handler();
    Runnable r=new Runnable() {
        public void run() {
            //what ever you do here will be done after 3 seconds delay.              
        }
    };
    handler.postDelayed(r, 3000);
    
    0 讨论(0)
  • 2020-12-14 08:25

    You shouldn't ever block the ui thread with a sleep. Its ok to sleep on another thread, but even then it should be avoided. The right way to do this is to post a Runnable to a Handler. Then put whatever code you want to run after the delay in the run() method of the Runnable.

    0 讨论(0)
  • 2020-12-14 08:43

    You can define a Handle in your Activity and use Handle.postDelayed() from Activity's onCreate()so you receive a message on that handle in 3 seconds. Upon receving you can enable the button.

    You can do the same using AsyncTask where in doInBackground() you just sleep for 3 sec. Then in onPostExecute() you enable the button.

    0 讨论(0)
  • 2020-12-14 08:43

    Use object of Handler class and use method handler.postDelayed(thread,time).Don't use sleep() it will block ui thread.

    0 讨论(0)
提交回复
热议问题