AsyncTask is now translated as an asynchronous task
Overview:
AsyncTask makes the use of UI threads appropriate and simple. This class allows operations to be performed in the background and the processing results are displayed in the UI thread, without the operation thread.
An asynchronous task is a thread running in the background, and its running result is displayed in the UI thread.
An asynchronous task is defined by three generics and four steps.
Usage:
An AsyncTask must be inherited before it can be used. This subclass must override the doInBackground (Params...) method, and usually rewrite the onPostExecute (Result) method.
Below is a subclass column from the official documentation:
Private class DownloadFilesTask extends AsyncTask <URL, Integer, Long> {
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 ));
}
Return totalSize;
}
Protected void onProgressUpdate (Integer... progress ){
SetProgressPercent (progress [0]);
}
Protected void onPostExecute (Long result ){
ShowDialog ("Downloaded" + result + "bytes ");
}
}
Private class DownloadFilesTask extends AsyncTask <URL, Integer, Long> {
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 ));
}
Return totalSize;
}
Protected void onProgressUpdate (Integer... progress ){
SetProgressPercent (progress [0]);
}
Protected void onPostExecute (Long result ){
ShowDialog ("Downloaded" + result + "bytes ");
}
}
Once created, a task can be easily executed:
New downloadfilestask(cmd.exe cute (url1, url2, url3 );
New downloadfilestask(cmd.exe cute (url1, url2, url3 );
Generic AsyncTask:
The three types are as follows:
1. Params: parameter type entered during task execution
2. Progress: Percentage Progress of background task execution
3. Result: The Result returned after the background task is executed, such as String
AsyncTask:
When an asynchronous task is executed, perform the following four steps:
1. onPreExecute (), which is called by the UI thread immediately after the task is executed. This step is usually used to set this task. For example, a progress bar is displayed on the user interface.
2. doInBackground (Params...) runs immediately in the background after onPreExeecte is executed. This step is usually used to perform time-consuming operations on the background. The task parameters are passed to this step, and the calculation result is returned and passed to the last step. In this step, you can also call publishProgress (Progress...) to update the task Progress. These values will be published by the UI thread during onProgressUpdate (Progress.
3. onProgressUpdate (Progress). After publisProgress (Progress...) is called, it is triggered by the UI thread, and its execution time is uncertain. This method is used to display the execution progress of background operations on the user interface. For example, it can be used to dynamically display a progress bar or display logs in the text box.
4. OnPostExecute (Result) is called by the UI thread after the background operation is completed. In this step, the Result of the background operation is passed to it as a parameter, that is, Result.
Cancel a task:
By calling cancel (boolean), a task can be canceled at any time. If you call this method later, isCancelled () will return true. After this method is called ...) onPostExecute (object) is not called but onCancelled (object ). to cancel a task as soon as possible, you must periodically check the returned values of isCancelled () in doInBackgrouond (object. If possible, for example, you can do this in a loop.
Note:
The task instance must be created in the UI thread.
Execute (Params...) must be called in the UI thread
Do not manually call onPreExecute (), onPostExecute (Result), doInBackgrouond (Params...), onProgressUpdate (Progress ..)
The task is executed only once. If the task is executed for the second time, an exception is thrown.
From thick and thin hair, water comes to the fore