Basic use of Android Asynctask

Source: Internet
Author: User

1. First write a class inheriting asynctask such as: Class Downloadtask extends Asynctask<parms, Process, Result>

Parms is the execution of Asynctask incoming parameters, such as: Task.execute ("First parameter", "Second parameter", "ETC.") Such as:

asynctask<string,void,void> task = new asynctask<string,void,void>public Void doinbackground (String ...  params) {for (String parameter:params) {log.i (TAG, "Received parameter:" + parameter);  } return null; }};

Process is the display of the current progress, such as

public void Onprogressupdate (Integer ... params) {int progress = params[0]; Gestationprogressbar.setprogress (progress);}

Result is the return of the result when the task has finished executing. For example:

Private class Fetchitemstask extends asynctask<void,void,list<galleryitem>>{        @Override        Protected list<galleryitem> doinbackground (Void ... params) {            return new FLICKRFETCHR (). Fetchitems ();        }}

2. To replicate the methods as needed, there are several ways to do this:

OnPreExecute () This method is called before the background task begins execution, and is used for initialization of some interfaces, such as displaying a progress bar dialog box, and so on.

Doinbackground (Params ...) All the code in this method will run in a sub-thread, and we should be here to handle all the time-consuming tasks. If you need to update UI elements, you can call Publishprogress (Progress ...). method to complete.

Onprogressupdate (Progress ...) When Publishprogress (Progress ...) is called in a background task Method, the method is called quickly, and the parameters that are carried in the method are passed in the background task.

OnPostExecute (Result) This method is called quickly when a background task is completed and returned through a return statement. The returned data is passed as a parameter to this method, and some UI actions can be made with the returned data.

3. If you want to start this task, simply write the following code: New Downloadtask (). Execute ();

Basic use of Android Asynctask

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.