Android中AsyncTask的使用详解

              在Android中实现异步任务机制有两种方式,Handler和AsyncTask。尤其在Android2.3以后,不允许在主UI中进行耗时操作——比如连接网络,进行socket通信等。
              Handler模式需要为每一个任务创建一个新的线程,任务完成后通过Handler实例向UI线程发送消息,完成界面的更新,这种方式对于整个过程的控制比较精细,但也是有缺点的,例如代码相对臃肿,在多个任务同时执行时,不易对线程进行精确的控制。AsyncTask是对Thread+Handler良好的封装,在android.os.AsyncTask代码里仍然可以看到Thread和Handler的踪迹
              为了简化操作,Android后来提供了工具类android.os.AsyncTask,它使创建异步任务变得更加简单,不再需要编写任务线程和Handler实例即可完成相同的任务。
在Android Develop API中是这样定义AsyncTask的:android.os.AsyncTask,同时给出这样的说明:
AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. An asynchronous task is defined by 3 generic types, called Params, Progress and Result, and 4 steps, called onPreExecute, doInBackground, onProgressUpdate and onPostExecute.
              也就是说这个类可以更方便和容易的去更新UI线程,它在后台进行耗时操作并把结果返回给主UI线程却不需要额外的线程或者Handler。一个AsyncTask被定义了三种泛型类型的参数,分别代表“启动任务执行的输入参数”、“后台任务执行的进度”、“后台计算结果的类型”。这些参数如果没有被使用,可以用java.lang.Void类型代替。
              一个AsyncTask 的执行一般包括以下4个步骤:
            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. 在execute(Params... params)被调用后立即执行,一般用来在执行后台任务前对UI做一些标记。
            2. doInBackground(Params... 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. 在onPreExecute()完成后立即执行,用于执行较为费时的操作,此方法将接收输入参数和返回计算结果。在执行过程中可以调用publishProgress(Progress... values)来更新进度信息。
      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. 在调用publishProgress(Progress... values)时,此方法被执行,直接将进度信息更新到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.当后台操作结束时,此方法将会被调用,计算结果将做为参数传递到此方法中,直接将结果显示到UI组件上。
Note:
      1.The AsyncTask class must be loaded on the UI thread. This is done automatically as of JELLY_BEAN.
      The task instance must be created on the UI thread.
      2.execute(Params...) must be invoked on the UI thread.
      3.Do not call onPreExecute(), onPostExecute(Result), doInBackground(Params...), onProgressUpdate(Progress...) manually.
      4.The task can be executed only once (an exception will be thrown if a second execution is attempted.) 翻译过来就是说:
1.AsyncTask必须在UI线程中被加载,它的实例也必须在UI线程中被创建
2.execute(Params... params)方法必须在UI线程中调用。
3.不要手动调用onPreExecute(),doInBackground(Params... params),onProgressUpdate(Progress... values),onPostExecute(Result result)这几个方法。
4.不能在doInBackground(Params... params)中更改UI组件的信息。
5.一个任务实例只能执行一次,如果执行第二次将会抛出异常。

           


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值