Basic use of wirelessly asynctask

Source: Internet
Author: User

Basic use of wirelessly asynctask
    • Introduction to Usage scenarios

In Android development, we often need access to Internet resources that need to be done in a background thread because the Android UI thread does not allow time-consuming tasks to be performed. However, background threads are not allowed to modify the Android UI, so we generally use handler to modify the UI, which requires us to create new threads ourselves. In fact, wirelessly has provided us with a asynctask class that is used to handle time-consuming tasks, so we can do the same with this class.

    • Asynctask Class Introduction

Asynctask is an abstract class, and we generally integrate this class. This class needs to specify three generic parameters, the first of which is the type of the parameter to be passed when we start the task, and the second: the type of data returned when the task progresses, and the third: the type of data returned when the task completes. And we generally implement four of these methods, as follows: (where the Doinbackground method must be implemented)

 Public classMyasynctaskextendsAsynctask<string, Integer, string>{@Overrideprotectedstring Doinbackground (String ... params) {//TODO auto-generated Method Stubreturn NULL; } @Overrideprotected voidOnPreExecute () {//TODO auto-generated Method Stub        Super. OnPreExecute (); } @Overrideprotected voidOnPostExecute (String result) {//TODO auto-generated Method Stub        Super. OnPostExecute (Result); } @Overrideprotected voidonprogressupdate (Integer ... values) {//TODO auto-generated Method Stub        Super. Onprogressupdate (values); }}

These four methods are system automatic callback, do not need the user to call manually, the user when using this class is as follows:

New Myasynctask (); Myasynctask.execute ();

where the Execute () method can pass multiple arguments, the number of arguments is arbitrary, the type is the type specified by the first generic, (string specified here) in the Doinbackground method, the parameter is obtained in the form of: Params[0], params[1] ... The form. When the user calls the Execute () method, the system will call back the Doinbackground method (note that before this, the system calls the OnPreExecute method, and the user can initialize some other data or UI to suit their needs). After the task is completed, the system callbacks the OnPostExecute method, and the return value type is what we specify in the third generic type. So when does the Onprogressupdate method call? That is, the task has a new progress, in the Doinbackground method called Publishprogress (), the system can call back the method, where the parameters passed are arbitrary, the type is the second generic type specified. Here, the basic introduction of the Asynctask class is over.

    • Cancel a task

What if you encounter a situation where you need to cancel a task? The background task cannot be terminated directly, we can call Myasynctask.cancel (true), so that the status of the background thread is changed to cancel, the thread itself does not stop, just change his state, So we need to determine the state of the thread in the Doinbackground method, and if it is cancel, then return or break directly.

    @Override    protected  string Doinbackground (String ... params) {        // TODO auto-generated Method stub        if (iscancelled ()) {            returnnull;        }         // Handle other tasks        return "Data";    }

    • Add
    1. An instance of Asynctask must be created in the UI thread;
    2. The Execute () method must be called in the UI thread;
    3. The four overriding methods are automatically called, without having to call them manually;
    4. Each asynctask can only be executed once, and the exception will occur if executed more than once;

Basic use of wirelessly 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.