AsyncTask的链接在活动(回调?) [英] AsyncTask chaining in Activity (callback?)
问题描述
我想链数的AsyncTask
班一个活动里面
,那么它会显示另一个活动
一旦全部建成(成功)。目前,我链接它们赶走对方的 onPostExecute()
方法,但我不喜欢这个,因为它有些夫妇实施不必要的。
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.
例如,我有我的 ImportVideoActivity
该用户进入YouTube网址进行下载。要做到这一点,我有两个的AsyncTask
类:
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
:获取视频元数据信息,以及通过提供视频格式枚举(质量和codeCS) -
YouTubeVideoDownloadTask
:鉴于执行从视频信息元数据的URL下载影片
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.
我要的 ImportVideoActivity
来首先执行 YouTubeVideoInfoTask
来枚举视频质量,并挑选最好的一个。在此之后,它应该执行 YouTubeVideoDownloadTask
,完成之后启动 CropVideoActivity
通过下载的视频。
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.
我怎样才能优雅链里面的 ImportVideoActivity
这两个的AsyncTask
类?是否有某种监听器/回调,让我来监控这些任务的完成,并开始不使用 AsyncTask.onPostExecute()
方法下一个?
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?
推荐答案
是的。在活动中使用一个回调监听。只要这就是所谓的,你可以从你的活动开始新的AsyncTask的。
Yes. Use a Callback Listener in your Activity. As soon as this is called, you can start the new AsyncTask from your Activity.
在我的其他答案示例:<一href=\"http://stackoverflow.com/questions/23924483/how-to-return-an-object-from-the-asynctask-to-the-main-class-in-android/23924523#23924523\">How从AsyncTask的Android中返回一个对象主类
Example in my other answer: How to return an object from the asynctask to the main class in android
这篇关于AsyncTask的链接在活动(回调?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!