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?

  • ViewPager Fragments – Shared Element Transitions
  • Android Studio/Gradle not finding AppCompat resources for tests
  • How can I get the JSON response of a POST request in a WebView?
  • How can I catch SIGSEGV (segmentation fault) and get a stack trace under JNI on Android?
  • Android Studio Import Module Gradle Build Error
  • execution failed for task ':app:compileDebugNdk' failed to run this command ndk-build.cmd
  • 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();
        }
    }  
    

    Related posts:

    How to make a static button under a ScrollView?
    How to debug Google Glass GDK application?
    Android QRCode Scanner Library
    How to make apk Secure. Protecting from Decompile
    Android USB host-to-serial connection?
    How to pass array values to and from Android RenderScript using Allocations
  • Possibility to add parameters in button xml?
  • What is difference between DVM and ART ? Why DVM has been officially replaced with ART in Lollipop?
  • Set a cookie to a webView in Android
  • How to open/display documents(.pdf, .doc) without external app?
  • Android saving Bitmap to SD card
  • Espresso: Thread.sleep( );
  • 6 Solutions collect form web for “Using wait in AsyncTask”

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

    You can use Thread.sleep method

        try {
            Thread.sleep(1000);         
        } catch (InterruptedException e) {
           e.printStackTrace();
        }
    
    @Override
            protected String doInBackground(String... params) {
                // TODO Auto-generated method stub
                try {
                    Thread.currentThread();
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                return null;
    
            }
    

    If you’re looking to just postpone execution of a method for a set amount of time, a good option is Handler.postDelayed()

    define the handler and runnable…

    private Handler handler = new Handler();
    private Runnable runnable = new Runnable() {        
        finished();
    };
    

    and execute with delay…

    handler.postDelayed(runnable, MIN_SPLASH_DURATION);
    

    Use threads for this

    public class SplashActivity extends Activity{
    
    int splashTime = 5000;
    private Thread splashThread;
    private Context mContext;
    
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        this.mContext = this;
        setContentView(R.layout.splash_layout);
        splashThread = new Thread(){
            public void run() {
                try{
                    synchronized (this) {
                        wait(splashTime);
                    }
                }catch(InterruptedException ex){
                    ex.printStackTrace();
                }finally{
                    Intent i = new Intent(mContext,LocationDemo.class);
                    startActivity(i);
                    stop();
                }
            }
        };
    
        splashThread.start();
    }
    
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            synchronized (splashThread) {
                splashThread.notifyAll();
            }
        }
        return true;
    }
    

    on touch event, thread get notified.. can change according to your need.

    You have this way to work with asyntask and wait();

    public class yourAsynctask extends AsyncTask<Void, Void, Void> {
        public boolean inWait;
        public boolean stopWork; 
    
        @Override
        protected void onPreExecute() {
            inWait = false;
            stopWork = false;
        }
    
        @Override
        protected Void doInBackground(Void... params) {
            synchronized (this) {
                while(true) {
                    if(stopWork) return null;
                    if(youHaveWork) {
                        //make some
                    } else {
                        try {
                            wait();
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
            }
            return null;
        }
    
        public void mynotify() {
            synchronized (this) {
                if(inWait) {
                    notify();
                    inWait = false;
                }
            }
        }
    
        public void setStopWork() {
            synchronized (this) {
                stopWork = false;
                if(inWait) {
                    notify();
                    inWait = false;
                }
            }
        }
    }
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.