Using wait in AsyncTask

前端 未结 6 1529
逝去的感伤
逝去的感伤 2021-02-01 01:15

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

相关标签:
6条回答
  • 2021-02-01 01:50

    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.

    0 讨论(0)
  • 2021-02-01 01:53

    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);
    
    0 讨论(0)
  • 2021-02-01 01:56

    You can use Thread.sleep method

        try {
            Thread.sleep(1000);         
        } catch (InterruptedException e) {
           e.printStackTrace();
        }
    
    0 讨论(0)
  • 2021-02-01 02:02

    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;
                }
            }
        }
    }
    
    0 讨论(0)
  • 2021-02-01 02:06
    @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;
    
            }
    
    0 讨论(0)
  • 2021-02-01 02:11

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

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