Android-AnsyncTask asynchronous task, android-ansynctask

Source: Internet
Author: User

Android-AnsyncTask asynchronous task, android-ansynctask
Differences between synchronization and Asynchronization:

Synchronization, you must execute to complete a problem before you can continue to execute other.

Asynchronous, I will execute other problems first. After you finish the execution, you can return a result to me.

 

 

Why do android reference asynchronous tasks?

When android is started, it starts a thread, also known as the main thread and UI thread. However, we cannot hand over all time-consuming tasks to the main thread for completion, which will affect the user experience, in other words, we need to open up new threads to execute our tasks,

However, in android, (loading images and network programming are all time-consuming operations), network operations are prohibited in the main thread after android4.0, therefore, these time-consuming tasks must be handed over to sub-threads,

The problem is that ui operations cannot be performed in sub-threads. Only the ui thread of the main thread can perform ui operations. How can we return the information to users.

Method 1. We can use handler to write new operations in handler, and call sendmessage in the Child thread to notify our handler and the new ui

Method 2: Use Activity. runonUIThread (Runnable) to switch the sub-thread to the ui thread.

 

 

Why is AsyncTask used?

It is very simple, people are very lazy, and it is difficult to use the above two methods, so the big guys encapsulate an asynchronous execution method, so that we can directly process the time-consuming logic and update the UI.

We generally use it to perform network operations, image loading, data transmission, and other operations. (Of course we will select our third-party framework in the future)

 

Basic use of Asynctask:

It is an abstract class, that is, we need to rewrite its method and

AsyncTask <Params, progress, result>

Parames: The parameters executed when the task is started, such as the input url.

Progress: Percentage unit of background execution Progress

Result: The result returned after the asynchronous operation is completed.

(Compared with handler, his background is a thread pool, which reflects the advantage when data is huge)

 

 

Several important methods of AsyncTask

 

 @Override    protected void onPreExecute() {        super.onPreExecute();    }

// A method called before execution of time-consuming operations, which is equivalent to our Initialization Method

Note that this method is run in the ui thread

 

 

  @Override    protected String doInBackground(String... params) {        return null;    }

This party is responsible for executing our time-consuming business logic operations, that is, it runs in the Child thread. What should we do if we want to update the ui?

Call our publicProgress method to update our ui, which is run in the Child thread

 

publishProgress(Object o);

This method indicates the Update Progress, which can be used to update the download progress bar.

 

    @Override    protected void onProgressUpdate(Integer... values) {        super.onProgressUpdate(values);    }

This method is executed in the main thread. Once publicProgress is called in the time-consuming logic code, it indicates that we want to update the progress bar.

This method will be called to the ui thread and the new progress bar.

 

Note: The task instance must create task. excute (initial parameter such as url) in the ui thread );

We do not need to manually call the methods in the task.

The task can be executed only once.

 

 

For more information, see the csdn Piglet android basic knowledge article. If you are interested, we recommend that you read it.

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.