Android English document translation series (3)--asynctask

Source: Internet
Author: User

asynctask--Asynchronous task personally think this is a better translation. Class overview//Classes 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 have to manipulate threa DS and/or handlers.

Asynchronous tasks are more appropriate and simple to use in the UI thread, which allows a background operation to be performed and results are published to the UI thread without the need to manipulate threads and handler.

An asynchronous task was defined by a computation this runs on a background thread and whose result was published on the UI Thread. An asynchronous task was defined by 3 generic types, called Params , Progress and Result , and 4 steps, called onPreExecute , doInBackground , onProgressUpdate and onPostExecute .

An asynchronous task is defined in the operation of the UI thread that runs the background thread and the result return value, an asynchronous task that defines 3 generics, invokes the incoming parameter, executes the progress and executes the result, and calls these 4 steps: onPreExecute , and doInBackground onProgressUpdate onPostExecute .

Asynctask ' s generic types//generic parameters for asynchronous tasks

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

The three types that are called by an asynchronous task are as follows

    1. ParamsThe type of the parameters sent to the task upon execution.//parameter, this parameter will be sent to the execution body
    2. Progress, the type of the progress units published during the background computation.//progress, during the background operation this parameter will be continuously released
    3. ResultThe type of the result of the background computation.//results in background operation results

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

Not all types are to be used on asynchronous tasks, or they can be flagged as not needed and the parameter type changed to void.

Private class mytask extends asynctask<Void, void, void> { ...  }< /c6> The 4 steps//four parts

When a asynchronous task is executed, the task goes through 4 steps://as an asynchronous task executes, will walk

  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.//this method is called in the UI thread in either Call this function as soon as the service executes. This step usually sets a task, for example, to display a progress bar.
  2. doinbackground (Params ...) , invoked on the background thread immediately after  OnPreExecute ()  finishes executing. This step was used to perform background computation the can take a long time. The parameters of the asynchronous task is passed to this step. The result of the computation must is returned by this step and would be passed the last step. This step can also use  publishprogress (Progress ...)  to publish one or more units of progress. These values is published on the UI thread, in the  onprogressupdate (Progress ...)  step.//This method is executed at   OnPreExecute () executes the method immediately, which is used to perform a time-consuming task, and the parameters of the async task params are passed to this step. The result of the execution must be returned in this step and back to the last step, onpostexecute (Result). You can also use Publishprogress () to publish one or more of your progress here. These values will be in onprogressupdate (Progress ...)   method, routed to UI thread
  3. onProgressUpdate(Progress...), invoked the UI thread after a call to publishProgress(Progress...) . The timing of the execution is undefined. This method was used to display any form of progress in the user interface and the background computation is still execut Ing. For instance, it can is used to animate a progress bar or show logs in a text field.//this step to note, this method is called in publishProgress(Progress...)方法后,如果步调用 publishprogress () will not take a step oh. This method can display the progress of any style on the UI when the background task is still executing. For example, it can be used to update a progress bar, or to display text-based logs.
  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.//called when the asynchronous task finishes executing. The results of the execution are passed to this step as parameters.
 Threading rules//Threading Rules

There is a few threading rules that must being followed for this class to work properly:

This exhaustion must conform to the following rules in order to execute correctly

    • The task instance must be created the instance of the UI thread.//task must be built on the UI thread
    • execute(Params...)Must be invoked on the UI Thread.//execute () must also be called on the UI thread
    • onPreExecute()don't call,, onPostExecute(Result) , doInBackground(Params...) onProgressUpdate(Progress...) manually. Don't manually have to invoke these several methods:, onPreExecute() , onPostExecute(Result) doInBackground(Params...) , onProgressUpdate(Progress...) .
    • The task can be executed just once (an exception would be a thrown if a second execution is attempted.)//This task can be executed only once (if an attempt is made to the second time will throw an exception).
Memory observability//RAM Observation

Asynctask guarantees that all callback calls is synchronized in such a by that the following operations is safe without Explicit synchronizations. The asynchronous task must ensure that all callbacks are thread-safe and synchronized.

    • Set member The constructor or onPreExecute() , and refer to them in doInBackground(Params...) .
    • Set member doInBackground(Params...) in, and refer to them in and onProgressUpdate(Progress...) onPostExecute(Result) .

Android English document translation series (3)--asynctask

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.