One minute lets you learn to use Android Asynctask

Source: Internet
Author: User

Asynctask believe that most of the friends are already very familiar with its usage, here the record is mainly dedicated to those who have just contact with the Android or asynctask students, master please bypass.

Asynctask class is the Android1.5 version began to introduce, using it is mainly used to complete the processing of some asynchronous tasks, nonsense not much to say, directly on the code.

  

/*** asynctask<params, Progress, Result> three generic parameters * Params * need to be passed in when executing asynctask Parameters that can be used in a background task.     The first generic parameter in this example is specified as void, which means that no parameters are required to be passed to the background task when the Asynctask is executed. * Progress * When the background task executes, if you need to display the current progress on the interface, use the generic type specified here as the progress unit.     in this example , the second generic parameter is specified as Integer, which indicates that the integer data is used as the progress display unit. * Result * When the task finishes executing, if you need to return the result, use the generic type specified here as the return value type.     The third generic parameter in this example is specified as Boolean, which means that Boolean data is used to give feedback on the execution result. */    classMyTaskextendsAsynctask<void, Integer, boolean> {        /*** This method will be called between the background task start execution, for some interface initialization operation, such as displaying a progress bar dialog box. */@Overrideprotected voidOnPreExecute () {Super. OnPreExecute (); }        /*** All the code in this method will run in a sub-thread, and we should be here to handle all the time-consuming tasks. * Once the task is completed, the execution result of the task can be returned by the return statement, and if Asynctask's * third generic parameter specifies void, it can not return the result of the task execution. Note that in this method is not * can be UI operation, if you need to update UI elements, such as feedback the current task execution progress, you can call * publishprogress (Progress ...)         method to complete. */@OverrideprotectedBoolean doinbackground (Void ... params) {publishprogress (); return NULL; }        /*** When publishprogress (Progress ...) is called in a background task Method, the method is quickly called, and the parameters that are carried in the method are passed in the background task.         In this method, the UI can be manipulated, and the interface elements can be updated with the values in the parameter * number. */@Overrideprotected voidonprogressupdate (Integer ... values) {Super. Onprogressupdate (values); }        /*** This method will be called quickly when the background task is completed and returned through a return statement.         The returned data * is passed as a parameter to this method, and the returned data can be used to perform some UI actions, such as reminding the result of the task execution * and closing the Progress Bar dialog box. */@Overrideprotected voidOnPostExecute (Boolean aboolean) {Super. OnPostExecute (Aboolean); }    }

Start a task

New MyTask (). Execute ();

One minute lets you learn to use 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.