Use of AsyncTask for android Development

Source: Internet
Author: User

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.



Related Article

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.