android-Asynchronous Task-asynctask

Source: Internet
Author: User

What is an asynchronous task?

The asynchronous task is to open a sub-thread, then let it run, it will come back to tell you that it ran out, this is the result. This is similar to the callback in Java. Onsuccess () and OnError (), which we see very long in okhttp, are callback functions. Of course, this post does not discuss these callbacks, only the asynchronous task that comes with Android.

Android is officially banned from accessing the network in the main thread after the 4.0 version of the system.

The only way to solve this is to open a sub-thread and sub-thread to access the network. Just right, Google to Android wrote a convenient for the vast number of programmers to use the tool class, Asynctask. Let's take a look at how this asynchronous task is used.

First, write a class, inherit Asynctask, implement Asynctask is relatively simple, must implement only one method, that is Doinbackground () other can not need to implement, but usually we use the time is not only rewrite Doinbackground () also has other functions to match. Here's a look at what these functions are, and what each of them does.

Doinbackground ()

This method is executed in the sub-thread, and the only one of the many methods inside is executed by the child thread, which is usually done by the asynchronous task (that is, the time-consuming task), such as accessing the network, is written in this method.

OnPreExecute ()

This method is executed in the UI thread, see the method name also know the approximate, this method is called before Doinbackground (), that is, the preparation of asynchronous tasks, such as: access to the network before the progress bar, collect parameters to be passed and so on ...

OnPostExecute ()

This corresponds to the previous onpreexecute (). It is executed after Doinbackground () and is also executed in the UI thread. This is generally used to do the asynchronous task after the completion of operations, for example, JSON parsing (some of the larger JSON will also be in the Doinbackground () parsing), update the list, the text picture on the UI and so on ...

Onprogressupdate ()

Since there is a former, then a middle one. Yes, this method is a method of callback in the process of doinbackground () execution. What's the use of this? Existence is reasonable, for example, when we download the general will tell you how much to download, right. Also, this function is executed in the UI thread.

There are several other functions onCancel () and so on, these are not commonly used, can be slightly understood. There is no need to go deep into the view.

What do the return values and parameters of generics and methods mean?

function to understand what the role of the, look carefully, I have no parameters above, in fact, there are parameters, yes, yes, and parameters are very important. And we have 3 generic parameters when we inherit asynctask, what is this? Now let's look at the parameters of the generic parameters and methods and what the return values of the methods mean, and what are the links to each other?

Three generic types are: Params, Progress, Result

Params: So the name Incredibles, is the parameter, that is, the external passed in parameters, that is, we execute execute () this method passed in, at the same time, we can see that our generics and Doinbackground () The types of parameters that are accepted are consistent.

Progress: This parameter is also very well understood, is the progress, that is, the execution of the process callback, we can see Onprogressupdate () The type of parameters received and this is consistent, yes. That's it

Result : This parameter also knows what meaning, the result, that is, the result of the execution of the child thread, is also the type of the return value of Doinbackground (), and also the type of the parameter received by OnPostExecute ().

Here's a look at the relationship between the parameters:

Look at the red Arrow first, the red arrow indicates the type of the specified parameter limit, that is, when inheriting asynctask, these 3 parameter types actually limit doinbackground, Onprogressupdate, OnPostExecute the type of the received parameter and the type of the return value for these three functions .

Then look at the green Arrow, the green arrow represents the process of data transfer . The arguments passed from the start of execute are actually passed to the doinbackground inside, and Doinbackground returns the arguments that are actually passed to OnPostExecute.

The above two points are to be clear, but also beginners more difficult to understand, as for variable parameters, beginners can directly understand the array, for example, Doinbackground received a String ... You can understand it as string[], while the value is also used as an array of values, more variable parameters of the content click here.

Let's look at an example to see how we actually use it.

The code is nothing, it is through the log to track the execution of the entire asynctask process.

Here's a look at Asynctask's use and print logs:

In the case of use, a new asynchronous task is simply called, and execute () is invoked directly.

Finally, analyze the analysis log,

OnPreExecute () is actually executed before Doinbackground (), and we look at the parameters passed in, the parameters passed in, respectively, "123", "456", which are also taken out here. Then executes the code inside the sub-thread, the code is very simple, every 1 seconds to sleep to Onprogressupdate () to send the data, which can also be seen through the log, each receive the parameters are printed separately, and we sent the parameters are consistent. Finally, the execution is finished, the return is "true", the last line of the log is also true, which confirms the green arrow in front of us, the parameters of the process of passing.

The whole asynctask to this parse finished, there are a few points to explain.

1, Asynctask can only be executed once, can not be executed multiple times.

2, once the implementation, can not be canceled, the internet is said to cancel are cheats, are liars, can not be canceled, the only way to solve the request is not to do the data processing, that is, ignoring the data, as this time no request, this need to engage a flag to remember whether to handle the request back data.

android-Asynchronous Task-asynctask

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.