问题
I am trying to chain a few AsyncTask
classes inside a single Activity
, which will then show another Activity
once they are all completed (successfully). Currently I am chaining them off each others' onPostExecute()
method but I do not like this because it somewhat couples the implementations unnecessarily.
For example, I have my ImportVideoActivity
which the users enters a YouTube URL to be downloaded. To do so, I have two AsyncTask
classes:
YouTubeVideoInfoTask
: Gets the video metadata info as well as enumerating through available video formats (quality and codecs).YouTubeVideoDownloadTask
: Performs the video download given a URL from the video info metadata.
I want the ImportVideoActivity
to first execute a YouTubeVideoInfoTask
to enumerate the video qualities and pick the best one. After that, it should execute a YouTubeVideoDownloadTask
and upon completion start the CropVideoActivity
with the downloaded video.
How can I chain these two AsyncTask
classes elegantly inside of the ImportVideoActivity
? Is there some kind of listener/callback that allows me to monitor when these tasks complete and start the next one without using the AsyncTask.onPostExecute()
method?
回答1:
Yes. Use a Callback Listener in your Activity. As soon as this is called, you can start the new AsyncTask from your Activity.
Example in my other answer: How to return an object from the asynctask to the main class in android
来源:https://stackoverflow.com/questions/24194651/asynctask-chaining-in-activity-callback