android-Asynchronous Load Asynstask

Source: Internet
Author: User

We know that in Android, only the UI thread can update the UI, and the child threads cannot update the UI. But if an application needs to take some time-consuming action, if it does this on the UI thread, it will appear that the application is very slow, and this cannot be allowed to exist. So we thought about using a subroutine to do these time-consuming operations, but because the child threads couldn't update the UI, we didn't know when to update the UI.

To solve this problem, Android has an asynchronous load, and Asynctask is the purpose.

1. 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:

A.params: Type of input parameters when starting a task

B.progress: Type of progress value returned in background task execution

C.result: Type of return result after background task completes

2. Constructing callback methods for Asynctask subclasses

A.doinbackground: Must be overridden, and child threads take time-consuming action in this method

B.onpreexecute: Called before a background time-consuming operation is performed, usually in a method with some initialization operations

C.onpostexecute: When Doinbackground is complete, the system calls this method automatically and passes the value returned by the Doinbackground method to the method

D.onprogressupdate: When the Publishprogress method is called in the Doinbackground method, this method is triggered when the execution progress of the task is updated

3. Use Asynctask to load network pictures

1 classMyasynctaskextendsAsynctask<string, Void, bitmap>2     {3 4 @Override5         protectedBitmap doinbackground (String ... params) {6             Try {7Thread.Sleep (3000);8}Catch(interruptedexception e) {9 e.printstacktrace ();Ten             } OneBitmap Bitmap =NULL; AString urlstring = params[0]; -URLConnection URLConnection =NULL; -InputStream is =NULL; theURL url =NULL; -             Try { -URL =NewURL (urlstring); -URLConnection =url.openconnection (); +is =Urlconnection.getinputstream (); -Bufferedinputstream bis =NewBufferedinputstream (IS); +Bitmap =Bitmapfactory.decodestream (bis); A}Catch(malformedurlexception e) { at e.printstacktrace (); -}Catch(IOException e) { - e.printstacktrace (); -             } -             returnbitmap; -         } in  - @Override to         protected voidOnPreExecute () { +             Super. OnPreExecute (); - mprogressbar.setvisibility (view.visible); the         } *  $ @OverridePanax Notoginseng         protected voidOnPostExecute (Bitmap Bitmap) { -             Super. OnPostExecute (bitmap); the mprogressbar.setvisibility (view.gone); + Mimageview.setimagebitmap (bitmap); A         } the}

In Asynstask, only doinbackground runs on child threads, and other methods are in the main thread, so other methods can manipulate the UI

Here, we use the Java flow operation, the operation of its own convection is not very familiar with, so here remind yourself

4. Update the simulation progress bar

1 classMyasynctaskextendsAsynctask<void, Integer, void>2     {3 4 @Override5         protectedvoid Doinbackground (void ... params) {6              for(inti = 0; I < 100; i++)7             {8                 if(iscancelled ())9                 {Ten                     return NULL; One                 } A publishprogress (i); -                 Try { -Thread.Sleep (300); the}Catch(interruptedexception e) { - e.printstacktrace (); -                 } -             } +             return NULL; -         } +  A @Override at         protected voidonprogressupdate (Integer ... values) { -             Super. Onprogressupdate (values); -             if(iscancelled ()) -             { -                 return; -             } inMprogressbar.setprogress (values[0]); -         } to     } +  - @Override the     protected voidOnPause () { *         Super. OnPause (); $         if(Myasynctask! =NULL&& myasynctask.getstatus () = =AsyncTask.Status.RUNNING)Panax Notoginseng         { -Myasynctask.cancel (true); the         } +}

android-Asynchronous Load Asynstask

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.