创建doInBackground内另一AsyncTask的 [英] Creating another AsyncTask inside of doInBackground

查看:177
本文介绍了创建doInBackground内另一AsyncTask的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我经历了这么四下寻找答案<一href="http://stackoverflow.com/questions/7607399/executing-a-thread-inside-asynctasks-doinbackground">to这里同样的问题,但没有针对这个问题的问候这样做的危险性的答案。但基本上我想要运行另一个的AsyncTask 里面的另一个 doInBackground()方法的AsyncTask 。这是一个不错的办法和/或它留下任何潜在的副作用?

I recently looked through SO to find the answer to the same question here, but there was not an answer directed at the question in regards to the risk of doing so. But basically I want to run another AsyncTask inside the doInBackground() method of another AsyncTask. Is this a bad approach and/or does it leave any potential side effects?

我知道,在 onPostExecute()工程运行它,到目前为止,从过去的经验,我没有因为这样的事实有过任何问题的 onPostExecute()跑回到赖以起家的主线程上的的AsyncTask 开始。

I know that running it in the onPostExecute() works and so far from past experiences I have not had any issues due to the fact that onPostExecute() runs back on the main thread which started a AsyncTask to begin with.

推荐答案

从API文档:

•任务实例必须在UI线程上创建。

•The task instance must be created on the UI thread.

doInBackground()运行在后台线程。所以,你不能创建,并从doInBackground运行另一个AsyncTask的()。

doInBackground() runs on the background thread. So you cannot create and run another asynctask from doInBackground().

http://developer.android.com/reference/android/os/AsyncTask 。看一看在线程规则的话题。

当执行异步任务,任务经过4个步骤:(直接从DOC)

When an asynchronous task is executed, the task goes through 4 steps: (Straight from the doc)

1.on preExecute(),调用UI线程上执行任务之前。这个步骤是通过示出在用户界面的进度条通常用于设置任务,例如

1.onPreExecute(), invoked on the UI thread before the task is executed. This step is normally used to setup the task, for instance by showing a progress bar in the user interface.

2 doInBackground(参数...),在后台线程调用后立即preExecute()完成执行。这个步骤是用于执行后台计算,可以采取很长的时间。异步任务的参数被传递到这一步。计算的结果,必须通过此步骤被返回,将被传递回的最后一步。这一步也可以使用publishProgress(进展...)公布进展情况的一个或多个单元。这些值公布在UI线程上,在onProgressUpdate(进展...)一步。

2.doInBackground(Params...), invoked on the background thread immediately after onPreExecute() finishes executing. This step is used to perform background computation that can take a long time. The parameters of the asynchronous task are passed to this step. The result of the computation must be returned by this step and will be passed back to the last step. This step can also use publishProgress(Progress...) to publish one or more units of progress. These values are published on the UI thread, in the onProgressUpdate(Progress...) step.

3.onProgressUpdate(进展...),调用publishProgress(进展......)之后的UI线程调用。的执行的计时是不确定的。此方法用于显示的任何形式的用户界面中的进展而背景计算仍在执行。例如,它可以用于动画在文本字段进度条或显示日志

3.onProgressUpdate(Progress...), invoked on the UI thread after a call to publishProgress(Progress...). The timing of the execution is undefined. This method is used to display any form of progress in the user interface while the background computation is still executing. For instance, it can be used to animate a progress bar or show logs in a text field.

4.onPostExecute(结果),后台计算完成后在UI线程调用。背景计算的结果被传递给此步骤作为一个参数。

4.onPostExecute(Result), invoked on the UI thread after the background computation finishes. The result of the background computation is passed to this step as a parameter.

在首次推出,AsyncTasks是连续在一个后台线程中执行。与DONUT开始,此改变为线程允许多个任务,以在并行操作的一个池。与蜂窝出发,任务是在单个线程中执行,以避免由并行执行常见的应用程序的错误。

When first introduced, AsyncTasks were executed serially on a single background thread. Starting with DONUT, this was changed to a pool of threads allowing multiple tasks to operate in parallel. Starting with HONEYCOMB, tasks are executed on a single thread to avoid common application errors caused by parallel execution.

如果你真正想要并行执行,你可以调用executeOnExecutor(java.util.concurrent.Executor类,对象[])与THREAD_POOL_EXECUTOR。

您也可以考虑使用替代RoboSpice 的https:// github上。 COM / OCTO-在线/ robospice

可以使多个香料请求。在UI线程Notitifes当任务完成。值得一看的robospice。

Can make multiple spice request. Notitifes on the ui thread when task is complete. Worth having a look at robospice.

这篇关于创建doInBackground内另一AsyncTask的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆