Android 1.5: Asynctask doInBackground() not called when get() method is called
I am running into an issue with the way my asynctasks are executed. Here's the problem code:
firstTask = new background().new FirstTask(context);
if (firstTask.execute().get().toString().equals开发者_Go百科("1")) {
secondTask = new background().new SecondTask(context);
}
What I'm doing here is creating a new asynctask object, assigning it to firstTask and then executing it. I then want to fire off a separate asynctask when the first one is done and making sure it returns a success value (1 in this case). This works perfectly on Android 2.0 and up. However, I am testing with Android 1.5 and problems start popping up. The code above will run the first asynctask but doInBackground() is never called despite onPreExecute() being called. If I am to execute the first task without the get() method, doInBackground() is called and everything works as expected. Except now I do not have a way to determine if the first task completed successfully so that I can tell the second task to execute. Is it safe to assume that this is a bug with asynctask on Android 1.5? Especially since the API (https://developer.android.com/reference/android/os/AsyncTask.html#get%28%29) says that the get method has been implemented since API 3. Is there any way to fix this? Or another way to determine that the first task has finished?
If you are going to block (via get()
), why are you bothering with AsyncTask
in the first place? The whole point of AsyncTask
is to not block.
If you want to have SecondTask
execute when FirstTask
is done, have FirstTask
execute SecondTask
in FirstTask
's onPostExecute()
.
精彩评论