Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
349 views
in Technique[技术] by (71.8m points)

java - Can I chain async task sequentially (starting one after the previous asynctask completes)

Every time I do a httpRequest, the screen will appear to be locked up for a few seconds while the code is executing. Hence I used AsyncTask to do all my httpRequest stuff in a separate thread while putting up a ProgressDialog so the user knows something is happening.

I recently encountered the following situation: the input of one of my httpRequest is dependent on the result from a previous httpRequest (+parse) action. I can't just put the two AsyncTasks sequentially cause Android will put them in two threads and start the second one without the first one being finished. And without an appropriate input (the result of the first httpRequest), my second httpRequest will crash the app.

Is there way I can put-in a wait() to force the second AsyncTask not to start until the first one finishes?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I also had some same situation the other day. I had solved it in this way: Pass the reference of your activity to the constructor of async class and execute the do in background function. Now in post execute function call a public method of ur activity from async class to execute the task again... or try this:

            if (asynclass.getStatus() == android.os.AsyncTask.Status.PENDING) {
                asynclass.execute();
            } else if (RF.getStatus() == android.os.AsyncTask.Status.FINISHED) {
                asynclass = new asyncclass();
                asynclass.execute();
            } else {
                Toast.maketoast(this, "Plz wait", 1).show();
            }

Cheers


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...