从非UI线程的AsyncTask [英] Asynctask from non ui thread

查看:105
本文介绍了从非UI线程的AsyncTask的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

难道我们要启动它如果正确的话我使用的AsyncTask从非UI线程web服务接入(长期运行)从UI thread.Is执行asyctask一绝。
对不起,如果我的查询是错误的。

Is it a must that we have to initiate and execute asyctask from UI thread.Is it correct if i use asynctask for a webservice access(long running) from a non ui thread. sorry if my query is wrong.

在我的应用程序我要运行约10 web服务,并有上显示的用户界面。我很困惑,结果哪种方式会很好的AsyncTask,intentservice或每个Web服务调用创建线程并使其运行并行。

In my app i have to run around 10 webservices and have to show the result on ui .i am confused which approach will be good asynctask,intentservice or creating thread for each webservice call and making it to run parallel.

推荐答案

有一些必须遵循的AsyncTask正常工作的几个线程规则:


  1. 在AsyncTask的类必须在UI线程上加载。这是因为JELLY_BEAN的自动完成的。

  1. The AsyncTask class must be loaded on the UI thread. This is done automatically as of JELLY_BEAN.

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

The task instance must be created on the UI thread.

执行(参数...)必须在UI线程调用。

不要在preExecute(),onPostExecute(结果),doInBackground(参数...),onProgressUpdate(进展...)手动。

Do not call onPreExecute(), onPostExecute(Result), doInBackground(Params...), onProgressUpdate(Progress...) manually.

该任务可以只执行一次(如果第二次尝试执行一个异常将被抛出。)

The task can be executed only once (an exception will be thrown if a second execution is attempted.)

更多详情

在我个人的看法,我会建议使用AsyncTask的,因为它是高度运行后台任务进行了优化,并利用像多核处理器的好处。

In my personal opinion, I would suggest using AsyncTask as it is highly optimized for running background tasks and exploit benefits like multicore processor.

这篇关于从非UI线程的AsyncTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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