Android中AsyncTask的使用详解

来源:互联网 发布:网络电视需要什么 编辑:程序博客网 时间:2024/05/17 00:16
       在Android中实现异步任务机制有两种方式,Handler和AsyncTask。尤其在Android2.3以后,不允许在主UI中进行耗时操作——比如连接网络,进行socket通信等。
       Handler模式需要为每一个任务创建一个新的线程,任务完成后通过Handler实例向UI线程发送消息,完成界面的更新,这种方式对于整个过程的控制比较精细,但也是有缺点的,例如代码相对臃肿,在多个任务同时执行时,不易对线程进行精确的控制。AsyncTask是对Thread+Handler良好的封装,在android.os.AsyncTask代码里仍然可以看到Thread和Handler的踪迹
       为了简化操作,Android后来提供了工具类android.os.AsyncTask,它使创建异步任务变得更加简单,不再需要编写任务线程和Handler实例即可完成相同的任务。
在Android DevelopAPI中是这样定义AsyncTask的:android.os.AsyncTask,同时给出这样的说明:
AsyncTask enables proper and easy use of the UI thread. This classallows to perform background operations and publish results on theUI thread without having to manipulate threads and/or handlers.Anasynchronous task is defined by a computation that runs on abackground thread and whose result 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, onProgressUpdate andonPostExecute.
       也就是说这个类可以更方便和容易的去更新UI线程,它在后台进行耗时操作并把结果返回给主UI线程却不需要额外的线程或者Handler。一个AsyncTask被定义了三种泛型类型的参数,分别代表“启动任务执行的输入参数”、“后台任务执行的进度”、“后台计算结果的类型”。这些参数如果没有被使用,可以用java.lang.Void类型代替。
       一个AsyncTask 的执行一般包括以下4个步骤:
      1.onPreExecute()invoked onthe UI thread before the task is executed. This step is normallyused to setup the task, for instance by showing a progress bar inthe user interface. 在execute(Params...params)被调用后立即执行,一般用来在执行后台任务前对UI做一些标记。
      2.doInBackground(Params... params), invoked on thebackground thread immediately after onPreExecute() finishesexecuting. This step is used to perform background computation thatcan take a long time. The parameters of the asynchronous task arepassed to this step. The result of the computation must be returnedby this step and will be passed back to the last step. This stepcan also use publishProgress(Progress...) to publish one or moreunits of progress. These values are published on the UI thread, inthe onProgressUpdate(Progress...) step.在onPreExecute()完成后立即执行,用于执行较为费时的操作,此方法将接收输入参数和返回计算结果。在执行过程中可以调用publishProgress(Progress...values)来更新进度信息。
     3.onProgressUpdate(Progress...), invoked on the UI threadafter a call to publishProgress(Progress...). The timing of theexecution is undefined. This method is used to display any form ofprogress in the user interface while the background computation isstill executing. For instance, it can be used to animate a progressbar or show logs in a text field. 在调用publishProgress(Progress...values)时,此方法被执行,直接将进度信息更新到UI组件上。
     4. onPostExecute(Result), invoked on the UI thread after thebackground computation finishes. The result of the backgroundcomputation is passed to this step as aparameter.当后台操作结束时,此方法将会被调用,计算结果将做为参数传递到此方法中,直接将结果显示到UI组件上。
Note:
    1.TheAsyncTask class must be loaded on the UI thread. This is doneautomatically as of JELLY_BEAN.
    The taskinstance must be created on the UI thread.
   2.execute(Params...) must be invoked on the UI thread.
    3.Do notcall onPreExecute(), onPostExecute(Result),doInBackground(Params...), onProgressUpdate(Progress...)manually.
    4.The taskcan be executed only once (an exception will be thrown if a secondexecution is attempted.) 翻译过来就是说:
1.AsyncTask必须在UI线程中被加载,它的实例也必须在UI线程中被创建
2.execute(Params... params)方法必须在UI线程中调用。
3.不要手动调用onPreExecute(),doInBackground(Params...params),onProgressUpdate(Progress... values),onPostExecute(Resultresult)这几个方法。
4.不能在doInBackground(Params... params)中更改UI组件的信息。
5.一个任务实例只能执行一次,如果执行第二次将会抛出异常。

      


0 0
原创粉丝点击