Android Time consuming task _asynctask

Source: Internet
Author: User
<span id="Label3"></p>Asynctask is a lightweight sdk-encapsulated tool class for performing asynchronous tasks, so-called lightweight, which means that for tasks that can be done with one or two threads, Asynctask can be implemented very quickly and easily. But for an asynchronous task like a ListView that needs to be refreshed for every item, Asynctask is not so easy to do, and the effect is not good.<br><br>Here's a look at the basic structure of this class<br><pre name="code" class="java"><pre name="code" class="java">public class MyTask extends asynctask<params, Progress, result>{@Overrideprotected void OnPreExecute () { Super.onpreexecute ();} @Overrideprotected Result doinbackground (params ... params) {return null;} protected transient void onprogressupdate (progress[] values) {};p rotected void OnPostExecute (result Result) {};@ overrideprotected void oncancelled () {super.oncancelled ();} protected void oncancelled (result Result) {};}</pre></pre><br>As you can see from the code above, there are six ways to rewrite asynctask, and asynctask defines three generic type params, Progress, Result.<br>The Params is the input parameter that initiates the task execution, such as the URL of the HTTP Request.<br>Progress the percentage of background task Execution.<br>Result background performs the final return of the task, such as String.<br><br>These three types correspond to the types of incoming and returning values in the six overriding methods, and for convenience and extensibility, we typically define these three generic classes as Object.<br><br>OnPreExecute () runs on the main thread, here is the interface when the end user calls excute, and when the task executes before it starts calling this method, you can display the progress dialog box Here.<br>Doinbackground (Params ...) runs on a child thread, and the more time-consuming operations can be placed Here. Note that it is not possible to manipulate the UI Directly. You can call publicprogress (Progress ...) during Execution. To update the progress of the Task.<br>Onprogressupdate (Progress ...) Run on the main thread, publicprogress (Progress ...) This method is called after the call to display the progress of the task Execution.<br>OnPostExecute (Result) runs on the main thread to handle the end of a child thread, Doinbackground the method after completion of execution, and passes the return value of Doinbackground as a parameter to this method.<br>Oncancelled () runs on the main thread, the action to be done when the user calls Cancel.<br>Oncancelled (Object Result) This is the later API added IN.<br><br>With the Asynctask class, note the main thread and the child thread; Each overridden method is automatically called, and the task can only be executed once, otherwise an exception will occur when multiple calls are Made.<br>Forgot to say how this task was used:<br><pre name="code" class="java"><pre name="code" class="java">String params= ""; MyTask mytask=new mytask (); mytask.execute (params);</pre></pre><br><br><p><p><br></p></p><p><p><br></p></p> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>Android Time consuming task _asynctask</p></p></span>

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.