Android--Asynctask

Source: Internet
Author: User

In Android, there are two ways to solve the problem of direct communication between threads, one is through the mechanism of handler, and the other is the asynctask mechanism to be explained in detail today.

Asynctask

Asynctask, a lightweight asynchronous class provided by Android, can directly inherit Asynctask, implement asynchronous operations in a class, and provide interface feedback about the current level of asynchronous execution (UI Progress updates can be implemented via interfaces). The final feedback executes the result to the UI main thread.

How to understand Asynctask? In layman's terms, Asynctask is the equivalent of Android to provide us with a framework for multithreaded programming, between thread and handler, if we want to define a asynctask, we need to define a class to inherit Asynctask abstract class. , and implement its only one Doinbackgroud abstract method.

3 Generic parameters:

Asynctask <params, Progress, result>
    • Params: An input parameter that initiates the execution of a task, such as the URL of an HTTP request.
    • Progress: The percentage of task execution performed by the station.
    • Result: Results that the background performs the final return of the task, such as String.

If none is specified, it is written as void, for example:

Asynctask <void, Void, void>
  • OnPreExecute (): This method is executed before the asynchronous task executes, and is executed in the UI thread, usually in this method we do some initialization of the UI controls, such as popup to give ProgressDialog
  • Doinbackground (params ... params): This method is executed immediately after the OnPreExecute () method is executed, which is the method to handle the asynchronous task. The Android operating system will open a worker thread in the thread pool in the background to execute our method, so this method is executed in the worker thread, and after this method executes, we can send our execution results to our last OnPostExecute method, in this method, we can get data from the network and some time-consuming operation
  • onprogressupdate (Progess ... values): This method is also executed in the UI thread, and we sometimes need to return the progress of execution to our UI interface when the asynchronous task executes, such as downloading a picture of the web , we need to show the progress of their downloads at all times, and we can use this method to update our progress. Before this method is called, we need to invoke a publishprogress (Progress) method in the Doinbackground method to pass our progress to the Onprogressupdate method all the way to update
  • onpostexecute ( result ... result): When our asynchronous task executes, the result is returned to this method, which is also called in the UI thread, and we can display the returned results on the UI control
  • oncancelled (): the action to be made when the user calls Cancel

Code

 Public classYibuextendsAsynctask<integer, Integer, string> {    Private intnum = 0; PrivateTextView text;  PublicYibu (TextView textfrom) {//TODO Auto-generated constructor stub         This. Text =Textfrom; //        Pass in a TextView } @Overrideprotected voidOnPostExecute (String result) {//TODO Auto-generated method stubs        Super. OnPostExecute (Result); Text.settext ("Asynchronous Operation execution End" +result); } @Overrideprotected voidOnPreExecute () {//TODO Auto-generated method stubs        Super. OnPreExecute (); Text.settext ("Start asynchronous Operation"); } @Overrideprotected voidonprogressupdate (Integer ... values) {//TODO Auto-generated method stubs        Super. Onprogressupdate (values); intValue = Values[0];    System.out.println (value); } @OverrideprotectedString doinbackground (Integer ... param) {//TODO Auto-generated method stubs             for(; num<10;num++) {publishprogress (num);  //Call Onprogressupdate () }returnnum + param[0].intvalue () + ""; // call OnPostExecute, the value is passed directly in }}

Source

Code: HTTP://PAN.BAIDU.COM/S/1DD1QX01

Second.zip

Com.yuyidong.second.yibu.java

There is also an example of

Yibu.zip

Reprint Please specify source: http://www.cnblogs.com/yydcdut/p/3707960.html

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.