Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using wait in AsyncTask

When using a wait in an AsyncTask, I get ERROR/AndroidRuntime(24230): Caused by: java.lang.IllegalMonitorStateException: object not locked by thread before wait()

Is it possible to use an Asynctask just for waiting? How?

Thanks

class WaitSplash extends AsyncTask<Void, Void, Void> {     protected Void doInBackground(Void... params) {         try {             wait(MIN_SPLASH_DURATION);         } catch (InterruptedException e) {             // TODO Auto-generated catch block             e.printStackTrace();         }         return null;     }             protected void onPostExecute() {         waitSplashFinished = true;         finished();     } }   
like image 423
jul Avatar asked May 25 '11 10:05

jul


People also ask

How do I wait for async task to finish?

You will need to call AsyncTask. get() method for getting result back and make wait until doInBackground execution is not complete. but this will freeze Main UI thread if you not call get method inside a Thread.

Is AsyncTask deprecated?

This class was deprecated in API level 30.

What is the use of the AsyncTask class?

Android AsyncTask is an abstract class provided by Android which gives us the liberty to perform heavy tasks in the background and keep the UI thread light thus making the application more responsive. Android application runs on a single thread when launched.

Why use AsyncTask in android?

Android AsyncTask going to do background operation on background thread and update on main thread. In android we cant directly touch background thread to main thread in android development. asynctask help us to make communication between background thread to main thread.


2 Answers

Use Thread.sleep() instead of wait().

like image 101
Flo Avatar answered Oct 19 '22 21:10

Flo


You can use Thread.sleep method

    try {         Thread.sleep(1000);              } catch (InterruptedException e) {        e.printStackTrace();     } 
like image 21
Luis Zandonadi Avatar answered Oct 19 '22 22:10

Luis Zandonadi