AsyncTask源码翻译

作者:金牛区福生殡葬服务部 来源:www.cdfsbz.com 发布时间:2017-09-05 12:24:57
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.


译文:AsyncTask的正确实现,易于使用的用户界面线程。此类允许执行后台操作并把结果发布在UI线程上(主线程)而且使用handler.
*

AsyncTask is designed to be a helper class around {@link Thread} and {@link Handler} and does not constitute a generic threading framework. AsyncTasks should ideally be used for short operations (a few seconds at the most.) If you need to keep threads running for long periods of time, it is highly recommended you use the various APIs
provided by the java.util.concurrent package such as {@link Executor},{@link ThreadPoolExecutor} and {@link FutureTask}.


译文:异步任务帮助类的设计是围绕着线程和Handler的,但是并不构成通用线程的框架,异步任务应该用于很短的时间操作(几秒钟),如果你需要保持线程运行很长一段时间的话,你需要查看其他的各种api.

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, calledonPreExecute, doInBackground, onProgressUpdate and onPostExecute.


译文:一个异步任务由在后台线程运行的计算,其结果传给UI线程上。一个异步任务由3泛型类型定义,名为PARAMS,Progress和Result,和4个步骤,名为 onPreExecute(准备阶段),doInBackground(执行阶段),onProgressUpdate(进度更新阶段)和 onPostExecute(执行完成阶段)。
*
* Developer Guides
*

For more information about using tasks and threads, read the
* Processes and
* Threads developer guide.


*
译文:特别参考开发者手册,更多关于任务和线程的请阅读{@docRoot}guide/topics/fundamentals/processes-and-threads.html进程和线程开发者引导。

AsyncTask must be subclassed to be used. The subclass will override at least one method ({@link #doInBackground}), and most often will override a second one ({@link #onPostExecute}.)

Here is an example of subclassing:

* 译文:异步任务必须被子类继承并且必须实现至少一种方法(doInBackground)并且大多数将会覆盖第二种方法(onPostExecute)这里有一个例子如下: private class DownloadFilesTask extends AsyncTask { protected Long doInBackground(URL... urls) { int count = urls.length; long totalSize = 0; for (int i = 0; i < count; i++) { totalSize += Downloader.downloadFile(urls[i]); publishProgress((int) ((i / (float) count) * 100)); // Escape early if cancel() is called if (isCancelled()) break; } return totalSize; } protected void onProgressUpdate(Integer... progress) { setProgressPercent(progress[0]); } protected void onPostExecute(Long result) { showDialog("Downloaded " + result + " bytes"); } }

Once created, a task is executed very simply:

new DownloadFilesTask().execute(url1, url2, url3); *
译文:一次被创建,一个任务将会执行的非常简单(new DownloadFilesTask().execute(url1, url2, url3));
* AsyncTask’s generic types

The three types used by an asynchronous task are the following:


译文:异步任务的通用类型这里有三种被异步任务使用以下就是:
Params, the type of the parameters sent to the task upon execution. 译文:Params,是执行任务时发送的参数类型。
Progress, the type of the progress units published during the background computation. 译文:Progress,是后台计算过程中的进度单元类型。
Result, the type of the result of the background computation.
译文:Result,是后台线程计算得到结果的类型。

Not all types are always used by an asynchronous task. To mark a type as unused,simply use the type {@link Void}
译文:不是所有类型都被一个异步任务使用。要标记为未使用的类型,只需使用该类型
* private class MyTask extends AsyncTask { … }
*

The 4 steps
*

When an asynchronous task is executed, the task goes through 4 steps:

译文:当一个异步任务被执行时,任务就通过4个步骤来完成
* {@link #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. 译文:onPreExecute()在任务执行前,在用户界面上调用线程。这一步通常用于设置任务,例如通过在用户界面显示一个进度条。
* {@link #doInBackground}, invoked on the background thread immediately after {@link #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 {@link #publishProgress} to publish one or more units of progress. These values are published on the UI thread, in the {@link #onProgressUpdate} step. 译文:在onPreExecute()执行完后在后台线程上调用,这一步是用来执行后台耗时操作,可能耗费很长时间。异步任务的完成的结果必须返回在这一步,并将被传递到最后一步。这一步还可以使用publishProgress()这个方法吧进度传送给UI线程在,onProgressUpdate()这一步
{@link #onProgressUpdate}, invoked on the UI thread after a call to {@link #publishProgress}. 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. 译文:onProgressUpdate(),被UI线程调用在publishProgress这个方法之后,执行的时间是不确定的,这个方法被用来展示进度信息给用户,而后台任然在执行,建议实例它可用于在文本字段中动画一个进度条或显示日志。
{@link #onPostExecute}, 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线程完成之后,后台结果被传递到这一步。
Cancelling a task

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:武汉网站建设公司 https://www.jingchucn.com/zt/wuhan_wangzhanjianshe/

  • 上一篇:判断整数序列是否是二叉查找树的后序遍历
  • 下一篇:最后一页
  • 
    COPYRIGHT © 2015 金牛区福生殡葬服务部 ALL RIGHTS RESERVED.
    购买cdfsbz.com友情链接、项目合作请联系客服QQ:2500-38-100 邮箱:2500-38-100#QQ.com(#换@)
    本站所有原创信息,未经许可请勿任意转载或复制使用 网站地图 技术支持:肥猫科技
    精彩专题:网站建设
    购买本站友情链接、项目合作请联系客服QQ:2500-38-100