Use of Android asynctask

Source: Internet
Author: User

Organized from: http://blog.sina.com.cn/s/blog_9ec5eef701019s3t.html and http://www.apkbus.com/android-59747-1-1.html

Introduction

Asynctask makes it easier and more appropriate to use the UI thread. It can run some operations in the background and then display them on the UI without the need to operate the specific threads and handlers.
An asynchronous task consists of three basic types (call parameters, progress, and result), and four steps (call Start, Run in the background, process progress, and end)
), And most often will override a second one (onpostexecute (result ).)

Usage description
Asynchronous task must be used as a subclass,
The task instance must be created in the UI thread.
Execute (Params...) must be called in the UI thread
Do not manually call onpreexecute (), onpostexecute (result), doinbackground (Params...), onprogressupdate (Progress ...).
The task can only execute once. If it is executed multiple times, an exception is reported.

                                           
<Integer, integer, string>
Public class firstasynctask extendsasynctask <void, void, void> {
// The first parameter indicates the parameter type accepted by doinbackground.
// The second parameter defines the onprogressupdate Parameter
// The third parameter defines the doinbackground return value type and the onpostexecute parameter type
@ Override
Protected voiddoinbackground (void... arg0) // In an asynchronous thread, the UI cannot be updated. The three vertices indicate that the parameter is variable length, which may be an integer with two integers. arg0 indicates the input array.
{
Publishprogress (I); // call the onprogressupdate function to update the UI;
}

Protected voidonpreexecute () {}// called before executing the doinbackground method. You can update the UI in the UI thread.

Protected void onpostexecute (stringresult) // call after the doinbackground method is executed. You can update the UI in the UI thread.

Protected void publishprogress (){}

Protected void onprogressupdate (integer... arg1 ){}

}

Example
Class Definition
Private class downloadfilestask extends asynctask <URL, integer, long> {
Protected long doinbackground (URL... URLs ){
Int COUNT = URLs. length;
Long totalsize = 0;
For (INT I = 0; I <count; I ++ ){
Totalsize + = Downloader. downloadfile (URLs);
Publishprogress (INT) (I/(float) count) * 100 ));
}
Return totalsize;
}

Protected void onprogressupdate (integer... progress ){
Setprogresspercent (Progress [0]);
}

Protected void onpostexecute (long result ){
Showdialog ("downloaded" + Result + "bytes ");
}
}
Class usage
New downloadfilestask(cmd.exe cute (url1, url2, url3 );

Description of three basic types
Params: the type of parameters passed to the task.
Progress, indicating the type of the Progress Unit
Result, return type
Not all tasks need to define the type. If not, use void, as shown below:
Private class mytask extends asynctask <void, void, void> {...}

Four steps
Onpreexecute ():
Call immediately after the task is executed

Doinbackground (Params ...):
After onpreexecute is executed, run this method. The parameter is passed to this method. A value must be returned after execution. You can also use publishprogress (Progress ...) release progress to onprogressupdate (Progress ...), easy to update progress

Onprogressupdate (Progress ...):
After publishprogress (Progress...) is called, execute this method to display the progress information.
It usually displays a progress bar or log information in the text field.

Onpostexecute (result)
This method is executed after doinbackground (Params...) is executed.

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.