Asynctask profile is a lightweight asynchronous class provided by Android
You can directly inherit Asynctask to implement asynchronous operations in a class and provide interface feedback on the current level of asynchronous execution (UI Progress updates can be implemented via interfaces)
The final feedback executes the result to the UI main thread
Personally feel that this is on a thread based on the addition of UI control so it is not a simple thread derived class
This enriches the control of the UI
And the UI control is generally on the main thread on the ID is 1 as to exactly which interfaces within the Asynctask are on the main thread
What are the asynchronous threads and the order in which those interfaces are called? Let's take a look at an example
First write a asynctask derived class
The interfaces that perform the operation are rewritten and printed out
public class Playasynctask extends asynctask<void, void, void> {@Overrideprotected void Doinbackground (void ... arg0) {System.out.println ("Doinbackground Thread ID" +thread.currentthread (). GetId ()); The function generates a Onprogressupdate callback publishprogress (); return null;} @Overrideprotected void OnPostExecute (void result) {//TODO auto-generated method Stubsuper.onpostexecute (Result); System.out.println ("OnPostExecute Thread ID" +thread.currentthread (). GetId ());} @Overrideprotected void OnPreExecute () {//TODO auto-generated method Stubsuper.onpreexecute (); System.out.println ("OnPreExecute Thread ID" +thread.currentthread (). GetId ());} @Overrideprotected void onprogressupdate (void ... values) {//TODO auto-generated method Stubsuper.onprogressupdate ( values); System.out.println ("Onprogressupdate Thread ID" +thread.currentthread (). GetId ());}}
An object is generated on the activity so that an asynchronous operation is performed on the main thread
System.out.println ("asynctask start"); Playasynctask playasynctask = new Playasynctask (); Playasynctask.execute ();
Look at the results of the output
What you can see is that only Doinbackground will execute on the async thread and the rest is on the main thread
Which means that other callback functions, except Doinbackground, can perform actions on the UI.
Use of Android development Asynctask