1. Asyntask class Structure
Several of the internal callback functions used by the Asystask class are:
Doinbackground ()
OnPreExecute ()
OnPostExecute ()
Onprogressupdate ()
Copy Code
It is these callback functions that make up the use logical structure of the Asyntask class.
Note: Each asyntask subclass must replicate at least the Doinbackground () method.
2. Callback Logic Relationship
Call relationships such as:
After the 1> main thread calls the Execute () method of the Asyntask subclass instance, the OnPreExecute () method is called first. OnPreExecute () runs in the main thread and can be used to write some start prompt code.
2> starts a new thread and calls the Doinbackground () method to perform asynchronous data processing.
When the 3> finishes processing, the asynchronous thread ends and the OnPostExecute () method is called in the main thread. OnPostExecute () can perform some end-of-tip processing.
Supplemental: When the Doinbackground () method is processed asynchronously, if you want to notify the main thread of some data (e.g., processing progress). At this point, you can call the Publishprogress () method. At this point, the main thread calls the Onprogressupdate () method of the Asyntask subclass for processing.
3, the transfer of data between various functions
Through the above call relationship, we can probably see some data transfer relationship. As follows:
Execute () is passed to Doinbackground ().
The return value of Doinbackground () is passed to OnPostExecute ().
Publishprogress () is passed to Progressupdate ().
Important: In order to invoke a relationship that is explicit and secure, the Asyntask class passes 3 generics when inheriting. The first generic corresponds to the delivery type of execute () to Doinbackground (). The second generic corresponds to the return type of Doinbackground () and the type passed to OnPostExecute (). The third generic corresponds to the type that publishprogress () passes to Progressupdate ().
The data passed is an array of the corresponding type, and the arrays are variable-length oh. Can be used according to the specific circumstances.
5. Summary
It may seem complicated to see this asynchronous call relationship for the first time, but when you're familiar with it, you'll find that this structure works well. This structure encapsulates all the thread traffic as callback functions, and the invocation logic is easy to write. In particular, after the end of asynchronous processing, there are callback functions for finishing. If you are using the thread's Run () method, the run () does not return a value after the end. So you have to set up a communication mechanism. However, in fact, the use of handler+thread mechanism can actually replace Asyntask's call mechanism. As long as the handler object is passed to thread, it can be easily processed asynchronously. And this MVC pattern structure is more obvious, convenient management. So I think, the use of asyntask or handler+thread structure, personal preferences bar. But one thing you can clearly feel is that handler+thread is suitable for large-frame asynchronous processing, while Asyntask is suitable for small, simple asynchronous processing. All of these are personal points of view + understanding. A new point of view please point out.
Asynchronous task class inherits three generic types, the first type is the parameter type when excute in the main thread, and also the parameter type of the Doinbackground method The second parameter type is the type of the parameter when the Publishprogress method is called in the Doinbackground method, and also the onprogressupdate parameter type, and the third parameter type is the return value type of Doinbackground. is also the parameter type of the OnPostExecute method (also the oncancelled (T result) method parameter type, which requires more than API 11).
Android Asynchronous Task Asyntask (1)