AsyncTask——异步任务

 
个人认为这是翻译比较好的一次。。

Class Overview//类概述

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.

//异步任务用在UI线程更适当和简单,这个类允许执行一个后台运作并且发布结果到UI线程而不需要去操作线程和Handler。

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 ParamsProgress and Result, and 4 steps, calledonPreExecutedoInBackgroundonProgressUpdate and onPostExecute.

//一个异步任务被定义在运行后台线程和结果返回值UI线程的操作里面,一个异步任务定义了3个泛型,调用传入参数、执行进度和执行结果,并调用这4步:onPreExecutedoInBackgroundonProgressUpdate and onPostExecute.

AsyncTask's generic types//异步任务的泛型参数

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

//这个三个被异步任务调用的类型如下

  1. Params, the type of the parameters sent to the task upon execution.//参数,这个参数将发送至执行体
  2. Progress, the type of the progress units published during the background computation.//进度,在后台操作期间这个参数将被不断发布出去
  3. Result, the type of the result of the background computation.//结果,后台运作的结果

Not all types are always used by an asynchronous task. To mark a type as unused, simply use the type Void:

//不是所有的类型都要用在异步任务上,也可标志为不需使用,参数类型改为Void即可。

private class MyTask extends AsyncTask<Void, Void, Void> { ... }

The 4 steps//四大部

When an asynchronous task is executed, the task goes through 4 steps://当一个异步任务执行了,将会走一下

  1. onPreExecute(), invoked on the UI thread immediately after the task is executed. This step is normally used to setup the task, for instance by showing a progress bar in the user interface.//这个方法调用在UI线程在任务执行了就立即调用这个函数。这一步通常设置一个任务,例如可以显示一个进度条。
  2. doInBackground(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()执行完毕就立即执行此方法,这步用以执行一个耗时的任务,异步任务Params的参数将传至这一步,执行的结果必须在这一步返回并回传至最后一步即onPostExecute(Result)。在这里也可以用publishProgress()发布一个或多个进度。这些值将在onProgressUpdate(Progress...)  方法里,传送至UI线程
  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...)方法后,如果步调用publishProgress()将不走一步哦。这个方法可以在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.//这一部是在异步任务执行完时调用。执行的结果将以参数的形式传递至这一步。

Threading rules//线程规则

There are a few threading rules that must be followed for this class to work properly:

//这个累必须符合以下规则才能正确的执行

 

Memory observability//内存观察

AsyncTask guarantees that all callback calls are synchronized in such a way that the following operations are safe without explicit synchronizations.异步任务必须保证所有的回调都是线程安全的同步的。

Android英文文档翻译系列(3)——AsyncTask的更多相关文章

  1. Android英文文档翻译系列(1)——AlarmManager

      原文:个人翻译,水平有限,欢迎看官指正.                                                              public class Ala ...

  2. Android英文文档翻译系列(6)——LocalBroadcastManager

    public class LocalBroadcastManager extends Object java.lang.Object    ↳ android.support.v4.content.L ...

  3. Android英文文档翻译系列(5)——VPNService

      API14位于android.net.VpnService 类概述|Class OverviewVpnService is a base class for applications to ext ...

  4. Android英文文档翻译系列(4)——PopupWindow

    public class PopupWindow extends Object //直接继承至Object java.lang.Object ↳ android.widget.PopupWindow  ...

  5. Android英文文档翻译系列(2)——HandlerThread

      public class   HandlerThread extends Thread   Class  Overview Handy class for starting a new threa ...

  6. Android异步处理系列文章四篇之四 AsyncTask的实现原理

    Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面Android异步处理二:使用AsyncTask异步更新UI界面Android异步处理三:Handler+Loope ...

  7. Android异步处理系列文章四篇之二 使用AsyncTask异步更新UI界面

    Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面Android异步处理二:使用AsyncTask异步更新UI界面Android异步处理三:Handler+Loope ...

  8. 【转载】Android异步处理系列文章

    本博文地址:http://blog.csdn.net/mylzc/article/details/6777767 转载请注明出处. 为了给用户带来良好的交互体验,在Android应用的开发过程中需要把 ...

  9. Android之Activity系列总结(一)--Activity概览

    Activity 本文内容 创建 Activity 实现用户界面 在清单文件中声明 Activity 启动 Activity 启动 Activity 以获得结果 结束 Activity 管理 Acti ...

随机推荐

  1. Winform快速导出

    public static void ExportExcel(DataGridView DataGridView01){ Stream stream = null; StreamWriter writ ...

  2. 【Unity笔记】Behaviour Designer的使用方法

    Tasks列表 -- Composites选项 Sequence:图标是“箭头”,相当于And逻辑.下接多个子任务,它们从左到右依次执行.所有子任务执行成功,则Sequence返回成功:任一子任务执行 ...

  3. Hadoop计算中的Shuffle过程(转)

    Hadoop计算中的Shuffle过程 作者:左坚 来源:清华万博 时间:2013-07-02 15:04:44.0 Shuffle过程是MapReduce的核心,也被称为奇迹发生的地方.要想理解Ma ...

  4. [uart]UART中的硬件流控RTS与CTS

    转自:http://blog.csdn.net/zeroboundary/article/details/8966586 在RS232中本来CTS 与RTS 有明确的意义,但自从贺氏(HAYES ) ...

  5. linux 中的进程wait()和waitpid函数,僵尸进程详解,以及利用这两个函数解决进程同步问题

    转载自:http://blog.sina.com.cn/s/blog_7776b9d3010144f9.html 在UNIX 系统中,一个进程结束了,但是他的父进程没有等待(调用wait / wait ...

  6. Apache Httpd 反向代理配置 (笔记)

    Apache Httpd 配置Http反向代理 打开配置文件 httpd.conf 先启动相关模块(去掉前面的注释#)LoadModule proxy_module modules/mod_proxy ...

  7. 【SpringMVC笔记】第四课 注解的处理器映射器和处理器适配器使用

    一.注意点: 版本问题 spring3.2以前的版本,注解的映射器和适配器使用以下两个类. org.springframework.web.servlet.mvc.annotation.Default ...

  8. hdu 1595 find the longest of the shortest(dijstra + 枚举)

    http://acm.hdu.edu.cn/showproblem.php?pid=1595 大致题意: 给一个图.让输出从中删除随意一条边后所得最短路径中最长的. . 思路: 直接枚举每条边想必是不 ...

  9. mongodb查询之从多种分类中获取各分类最新一条记录

    mongodb查询之从多种分类中获取各分类最新一条记录 2017年04月06日 13:02:47 monkey_four 阅读数:6707更多 个人分类: MongoDBJavaScript   文章 ...

  10. Hibernate 注解中CascadeType用法汇总

    这两天,参加一个课程设计,同时这个项目又作为一个模块镶嵌到其他项目中,考虑如此,应与原先的架构相同,因牵扯到留言和相互@功能,故数据库之间OneToOne,OneToMany,ManyToMany之风 ...