#Android application interface development

Chapter 3 Study

The third part

A pit trodden by AsyncTask in asynchronous processing

AsyncTask is one of the tools provided by Android, which can be easily used for child threads to update the UI. It is also an abstract class, and its methods need to be overwritten according to the three parameter types passed in the definition, but doInBackground() must be overwritten. You can use the getStatus() method to return the working status of a thread. For example, PENDING,RUNNING,FINISHED indicate preparing,RUNNING, or FINISHED.


Knowledge:

The following is an example of a custom AsyncTask class in the Android 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));
 // 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"); }}Copy the code

First, the method of rewriting:

  1. The onPreExecute() method is called before the background task starts to execute and is used to initialize the interface, such as displaying a progress bar dialog box.

  2. doInBackground(URL… All the code in this method runs in the child thread, where we should handle all the time-consuming tasks. Once the task is complete, the return statement returns the result of the task execution. If Void is specified as the third generic parameter of AsyncTask, the result of the task execution is not returned. Note that UI operations are not allowed in this method. If you need to update UI elements, such as reporting Progress for the current task, you can call publishProgress(Progress…). Method to accomplish.

  3. onProgressUpdate(Integer… Progress) when publishProgress is called in the background task (progress…) Method, the method is called immediately after the method, carrying parameters passed in the background task. In this method, you can manipulate the UI, using the values in the parameters to update interface elements accordingly.

  4. OnPostExecute (Long result) This method is called shortly after the background task has finished executing and is returned by a return statement. The returned data is passed to this method as an argument, and you can use the returned data to perform UI operations, such as reminding you of the results of a task or closing a progress bar dialog.

Obviously, the three generic parameters, < URL, Integer, Long >, specified at definition time, are the types received by the corresponding three methods in the overridden method. The order is as follows:

URL–>doInBackground(URL)–>publishProgress(Integer)–>onProgressUpdate(Integer), which uses Integer for UI updates. And URL–>doInBackground(URL)–>return Long–>onPostExecute(Long)

To start, use the following statement

new DownloadFilesTask().execute();
Copy the code

This is the first pit

When the inner child thread is FINISHED, the AsyncTask enters the onPostExecute() method and the state changes to FINISHED. This function cannot be called again

Cancel () is divided into soft cancel(false) and hard cancel(true). When using the first method, the system will automatically determine the timing of the cancellation. The second method is to cancel immediately, and the latter is not recommended. But!!!! In fact, whether it is soft or hard, AsyncTask may not eat… It didn’t work… He always stops when he likes… How can you control that? So… The determination of AsyncTask status becomes important…


Here, I hit the second hole

AsyncTask< Void, Integer, Void> where Integer is used to update the progress bar. Use this method in onProgressUpdate() :

Will that do? Since only one argument can be passed to onProgressUpdate(), use the String[] array as the middle argument:

AsyncTask<Void, String[], Void>
Copy the code

And then we use it to setProgress by casting the string that represents progress into an int. Okay? The above failed with Buldle? What about failed Map? failure

What the hell is that?! Can you use??


The examples of AsyncTask are all written exactly the same, with only one UI update such as Seekbar. Thinking all night, the next day …………

It worked! It turns out that the generic type in the middle of AsyncTask can support a limited number of types. (I guessed!) This is FINISHED when given 2 parameters. So, when you need to update multiple UIs, using AsyncTask should be considered.

– the –