AsyncTask

1、官网解释

(1)Class Overview

This classallows to perform background operations and publish results on the UI threadwithout having to manipulate threadsand/or handlers.

AsyncTask isdesigned to be a helper class around Thread and Handler and does not constitutea generic threading framework. AsyncTasks should ideally be used for short operations (a fewseconds at the most).

An asynchronoustask is defined by a computation that runs on a background thread and whoseresult is published on the UI thread. An asynchronous task is defined by 3 generic types, calledParams, Progress and Result, and 4 steps, called onPreExecute, doInBackground, onProgressUpdateand onPostExecute.

(2)

AsyncTask mustbe subclassed to be used. The subclass will override at least one method(doInBackground(Params...)), and most often will override a second one(onPostExecute(Result)).

(3)3 types

Params:the type of the parameters sent tothe task upon execution.

Progress:the type of the progress units publishedduring the background computation.

Result:the type of the result of the backgroundcomputation.

Not all typesare always used by an asynchronous task. To mark a type as unused, simply usethe type Void.

(4)There are a few threading rulesthat must be followed for this class to work properly:

•The AsyncTask class must be loaded on the UI thread.

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

•execute(Params...) must be invoked on the UI thread.

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

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

(5)doInBackground(Params…)

Override thismethod to perform a computation on a background thread. This method can callpublishProgress(Progress...) to publish updates on the UI thread.

(6)onPostExecute(Result)

Runs on the UIthread after doInBackground(Params...). The specified result is the valuereturned by doInBackground(Params...).This method won't be invoked if the taskwas cancelled.

(7)onPreExecute()

Runs on the UIthread before doInBackground(Params...).

(8)onProgressUpdate(Progress...values)

Runs on the UIthread after publishProgress(Progress...) is invoked.

(9)onCancelled(Result result)

Runs on the UIthread after cancel(boolean) is invoked and doInBackground(Object[]) hasfinished.

2、个人总结

(1)首先明确Android之所以有Handler和AsyncTask,都是为了不阻塞主线程(UI线程),且UI的更新只能在主线程(UI线程)中完成,因此异步处理是不可避免的。

(2)doInBackground(Params…)比较耗时的操作都可以放在这里。注意这里不能直接操作UI。此方法在后台线程执行,完成任务的主要工作,通常需要较长的时间。在执行过程中可以调用publishProgress(Progress…)来更新任务的进度。

(3)onPostExecute(Result)相当于Handler处理UI的方式,在这里面可以使用在doInBackground得到的结果处理操作UI。此方法在主线程执行,任务执行的结果作为此方法的参数返回。

(4)onPreExecute是在UI线程中执行,可以对UI空间进行操作。

(5)onProgressUpdate是在UI线程中执行,可以对UI空间进行操作。在调用publishProgress时,此方法被执行。

(6)onCancelled()当用户调用取消cancel()时,要做的操作。

3、AsyncTask  vs  Handler

(1)Handler模式需要为每一个任务创建一个新的线程,任务完成后通过Handler实例向UI线程发送消息,完成界面的更新。

(2)AsyncTask使创建异步任务变得更加简单,不再需要编写任务线程和Handler实例即可完成相同的任务。

(3)AsyncTask是对Thread+Handler良好的封装。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值