Multithreading for Android (I) asynctask

Source: Internet
Author: User

This article describes how to use asynctask.

First, we need to clarify the next concept, what is a UI thread. As the name implies, the UI thread is the one that manages the user interface!

Android UI thread operations are not secure, and operations that directly interact with the user interface must be performed in the UI thread. This mode is called the single thread mode. In single-thread mode, make sure that you do not block the UI thread or access the UI component only in the UI thread.

When we want to execute a complex and time-consuming algorithm and finally reflect the computing result to the UI, we will find that we cannot guarantee the above two requirements at the same time; we will surely think of opening a new thread to let this complicated and time-consuming task be executed in the background, but the execution is complete? We found that we could not interact with the UI.

To solve this problem, Android provides us with many solutions.

1) handler and message mechanisms: interaction with the UI through display throws and capture information;

2) activity. runonuithread (runnable): if the current thread is a UI thread, it will be executed immediately; otherwise, the thread operations in the parameter will be placed in the event queue of the UI thread, waiting for execution.

3) view. Post (runnable): puts the operation into the message queue. If the operation is put successfully, it will be executed in the UI thread and return true; otherwise, false is returned.

4) view. postdelayed (runnable, long) is basically the same as the third, but a delay time is added.

5) android1.5 provides a tool class for us to solve this problem.

 

Asynctask is an abstract class. asynctask defines three generic types: Params, SS, and result.
  

Input parameters for Params startup task execution, such as the URL of the HTTP request

.
Percentage of progress background tasks.

 

 

Result: The result returned when the task is executed in the background, such as string.

 

.

 

What developers need to do when using program calls is to implement these methods.


 

1) subclass asynctask

2) implement one or more of the following methods defined in asynctask

Onpreexecute (), this method will be called by the UI thread before the actual background operation is executed. You can make some preparations in this method, such as displaying a progress bar on the interface.

Doinbackground (Params...) will be executed immediately after the onpreexecute method is executed. This method runs in the background thread. Here, we will primarily perform those time-consuming background computing tasks. You can call publishprogress to update the real-time task progress. This method is an abstract method and must be implemented by sub-classes.

Onprogressupdate (Progress...), after the publishprogress method is called, UI thread will call this method to display the progress of the task on the interface, for example, display through a progress bar.

Onpostexecute (result). After doinbackground is executed, the onpostexecute method will be called by the UI thread, and the background computing result will be passed to the UI thread through this method.

 

To correctly use the asynctask class, the following guidelines must be followed:

1) The task instance must be created in the UI thread.

2) The execute method must be called in the UI thread.

3) do not manually call the onpreexecute (), onpostexecute (result), doinbackground (Params...), onprogressupdate (Progress...) methods.

4) The task can only be executed once. Otherwise, exceptions may occur during multiple calls.

 

 

 

Package cn.com. chenzheng_java; </P> <p> Import android. OS. asynctask; <br/>/** <br/> * @ author chenzheng_java <br/> * @ description asynchronous task acynctask example <br/> * <br/> */<br/> public class myasynctask extends asynctask <string, integer, object >{</P> <p>/** <br/> * this method is called by the UI thread, where you can access the UI components. <Br/> * in many cases, we will display a progress bar here to show that the background is performing <br/> * a function. <Br/> */<br/> @ override <br/> protected void onpreexecute () {<br/> super. onpreexecute (); <br/>}</P> <p>/** <br/> * this method is called by the background process for calculation of the main time consumption. <Br/> * this method is called after the onpreexecute method. Of course, in the execution process <br/> * we can call the publishprogress method every several seconds, update <br/> * progress information <br/> */<br/> @ override <br/> protected object doinbackground (string... params) {<br/> return NULL; <br/>}</P> <p>/** <br/> * after publishprogress is called in doinbackground, the UI thread will <br/> * call this method. You can dynamically change the progress of the progress bar here to let the user know <br/> * The current progress. <Br/> */<br/> @ override <br/> protected void onprogressupdate (integer... values) {<br/> super. onprogressupdate (values); <br/>}</P> <p>/** <br/> * after doinbackground is executed, it is called by the UI thread. You can <br/> * return the final result of our calculation to the user. <Br/> */<br/> @ override <br/> protected void onpostexecute (Object result) {<br/> super. onpostexecute (result); <br/>}</P> <p >}< br/>

 

 

Bytes ------------------------------------------------------------------------------------------

Asynctask is executed in four steps. Each step corresponds to a callback method.

 

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.