Android--asynctask Asynchronous Task (i)

Source: Internet
Author: User

1. Why Asynchronous tasks

    • Android Single thread mode
    • Time-consuming operations are performed in a non-main thread (UI thread)

We all know that Android is a single-threaded mode, only the main thread can operate on UI, called the UI thread. Of course, the benefits are: to ensure the stability of the UI, accuracy, avoid multi-threaded simultaneous operation of the UI, resulting in UI confusion
But at the same time Android is a multi-threaded operating system, it is impossible to put all the things on the main thread. If the task is blocked, the ANR (application not responding) error is thrown when the time is too long.


Why 2.AsyncTask is born

    • Updating the UI in a child thread
    • Encapsulating, simplifying asynchronous operations


3. Building parameters for the Asynctask sub-class
Asynctask<params,progress,result> is an abstract class that is typically used for inheritance, and inheriting asynctask needs to specify the following three generic parameters:

    • Params: Enter the type of the parameter when the task is started.
    • Progress: Type of progress value returned in background task execution
    • Result: The type of the result returned after the background execution task finishes

4. Constructing callback methods for Asynctask subclasses

    • doinbackground : You must override, asynchronously perform the tasks that the daemon will complete.
    • OnPreExecute : Called before the background time-consuming operation is performed, typically the user completes some initialization operations.
    • onpostexecute : When Doinbackground () completes, the system automatically calls the OnPostExecute () method and passes the value returned by the Doinbackground method to the method
    • onprogressupdate : The method is triggered after the Doinbackground () method calls the Publishprogress () method to update the execution progress of the task

Now let's write a test class:

import Android.os.asynctask;import Android.util.Log; Public classAsynctasktest extends Asynctask<void, Void, void>{@Overrideprotectedvoid Doinbackground (void ...params) {log.i ("Task","Doinbackgroun"); return NULL; } @Overrideprotected voidOnPostExecute (Void result) {LOG.I ("Task","OnPostExecute");    Super.onpostexecute (result); } @Overrideprotected voidOnPreExecute () {LOG.I ("Task","OnPreExecute");    Super.onpreexecute (); } @Overrideprotected voidonprogressupdate (Void ... values) {LOG.I ("Task","onprogressupdate");    Super.onprogressupdate (values); }}

References in mainactivity:

protected void onCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);         New asynctasktest ();        Task.execute ();    }

When we run on the emulator:

According to the fourth above, we call the Publshprogress method in Doinbackground, and after running, we find one more onprogressupdate

protected void Doinbackground (void ... params) {                log.i ("Task", "Doinbackgroun");        Publishprogress (params);         return NULL ;    }

Thank you for your attention. I love that you don't for who is, but is for who I am before.

Android--asynctask Asynchronous Task (i)

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.