Inquiry Service (two) - AsyncTask

In order for us to operate in sub-UI thread, Android provides AsyncTask to help us. The principle behind AsyncTask is based on asynchronous message processing mechanism.

AsyncTask basic usage

AsyncTask is an abstract class, we have to create a subclass to inherit it. Upon succeeding to develop our three generic parameters.
Params need to pass parameters when performing AsyncTask can be used for background services.

When the Progress background task, if you need to display the progress of the current task on the page, using the generic parameters here to develop as the progress of the unit.
Result When the task is finished, if you need to return the tasks, the use of generics here to develop as a return value type.
One of the most simple custom AsyncTask can be written as follows:

class DownloadTask extends AsyncTask<Void,Integer,Boolean>{
....
}

Here said they did not pass parameters when performing AsyncTask, displaying the progress of the units of Integer, Boolean type return after the task is completed.

We need to rewrite several methods of AsyncTask can customize complete the task, the general method needs to be rewritten, there are four.

1, onPreExecute () before the start of a background task calling for a number of operations to initialize the interface.
2, doInBackground (Params ...) code to run this method in which the child thread, here we naturally want to deal with time-consuming operation, the task is completed the results returned by the Return statement. Inside this method, we can not carry out the operation UI, UI if we need to operate, then we can be done by publishProgress (Progress ...).
After 3, onProgressUpdate (Progress ...) when inside we doInBackground (Params ...) method performs the publishProgress (Progress ...) statement, onProgressUpdate (Progress ...) will soon be executed, the process parameters is carried from a background task passed over. Inside this method, we can UI operation, the numerical parameters can be updated accordingly to the interface element.
4, onPostExecute (Result) When the background task execution ends and returns through the Return statement, this method is called, the same data can be used to return some UI operations, such as to remind the results of task execution or close the progress bar and so on.

AsyncTask summary

Use AsyncTask, time-consuming operation in which doInBackground () method, UI operations inside onProgressUpdate () method to perform the task of finishing work on the inside onPostExecute () method.

AsyncTask task start

Start this task, we only need to add the following code:

newDownloadTask().execute();

These are the basic usage AsyncTask, we do not need to consider the asynchronous message processing mechanism, they do not create a Handler object to process the message, just call publishProgress () method, you can achieve switching from the child thread to the main thread.

发布了37 篇原创文章 · 获赞 10 · 访问量 1万+

Guess you like

Origin blog.csdn.net/OneLinee/article/details/78569446