Use of AsyncTask for android Development
Zookeeper
AsyncTask is a lightweight asynchronous class provided by android.
AsyncTask can be directly inherited to implement asynchronous operations in the classAndProvide interface feedback to the currentAsynchronous execution degree(UI progress update can be implemented through the interface)
Finally, the execution result is fed back to the main UI thread.
I personally think this is based on a thread and adds control over the UI. This is not a simple thread derived class.
This enriches the UI control.
The UI control is generally on the main thread. ID is 1. What interfaces are in the AsyncTask?
What are asynchronous threads and the call sequence of those interfaces? Let's take a look at an example.
First, write an AsyncTask derived class.
Which of the operations are overwritten and output?
Public class PlayAsyncTask extends AsyncTask
{@ Overrideprotected Void doInBackground (Void... arg0) {System. out. println ("doInBackground Thread ID" + Thread. currentThread (). getId (); // This function will generate an 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 executed on the main thread.
System.out.println(" AsyncTask start"); PlayAsyncTask playAsyncTask = new PlayAsyncTask(); playAsyncTask.execute();
View the output result
We can see that only doInBackground will execute the rest in the asynchronous thread on the main thread.
That is to say, all callback functions except doInBackground can perform operations on the UI.