Android asynchronous load full parsing using Asynctask

Source: Internet
Author: User

Android asynchronous load full resolution usage Asynctask overviewSince we mentioned multithreading, we have to mention the thread pool, which can not only manage concurrent threads, but also improve the efficiency of their execution and optimize the entire app. Of course we can create a thread pool by ourselves, but it's annoying to create an efficient thread pool, but the Android system gives me a Asynctask class to help us quickly implement multithreaded development, and its underlying implementation is actually a thread pool.
Preliminary study on AsynctaskAsynctask, as the name implies, is used to do asynchronous processing. With Asynctask, we can easily solve the problem of multi-threading, as well as the communication between multithreading.Asynctask parametersAbstract class Asynctaskhas three generic parameters:
Asynctask <params, Progress, result>

    • Params: This generic specifies the type of arguments we pass to the asynchronous task when it executes
    • Progress: This generic specifies the type of parameters that our asynchronous task will execute when it executes the progress returned to the UI thread
    • Result: The type of results returned to the UI thread after this generic specified asynchronous task finishes executing
We are creatingAsynctask, you need to specify these three parameters.
AsynctaskMethod asynctask hasSuch a few abstract methods:
OnPreExecute (): This method is executed before the asynchronous task executes, and is executed in the UI thread, usually we do some initialization of the UI control in this method, such as popupProgressDialogDoinbackground (params ... params): This method is executed immediately after the OnPreExecute () method is executed, which is the method to handle the asynchronous task. the Android operating system opens a worker thread in the thread pool in the background to execute our method, so this method is executed in the worker thread, After this method is executed, we can send our execution results to our last OnPostExecute method, SoDoinbackgroundIs the only method that must be overridden in these four methods.In this method, we can get data from the network and some time-consumingOperationonprogressupdate (Progess ... values): This method is also executed in the UI thread, and we sometimes need to return the progress of execution to our UI interface when the asynchronous task executes. For example, to download a Web image, we need to show the progress of its download at all times, and we can use this method to update our progress. Before this method is called, thewe need to call a publishprogress (Progress) method in the Doinbackground method to pass our progress all the way to the Onprogressupdate method So that you can update your progressOnPostExecute (Result ... result): When our asynchronous task executes, the result is returned to the method, which is also called in the UI thread. We can display the returned results on a UI control AsynctaskUseNow back in our example, we useAsynctaskTo download the image, of course, the way to download the image is to use the Getbitmapfromurl () method. First, we first write a Asyncdownloadimage class, inheriting Asynctask:
Private class Asyncdownloadimage extends Asynctask<string, Void, bitmap> {    private ImageView ImageView;    Private String URL;    Public Asyncdownloadimage (ImageView ImageView, String url) {        this.imageview = ImageView;        This.url = URL;    }    @Override    protected Bitmap doinbackground (String ... params) {        return Getbitmapfromurl (Params[0]);    }    @Override    protected void OnPostExecute (Bitmap Bitmap) {        super.onpostexecute (Bitmap);        if (Imageview.gettag (). Equals (URL)) {            imageview.setimagebitmap (bitmap);}}    }

This oneAsynctaskWe rewrote the doinbackground to download the image asynchronously, overriding the OnPostExecute to set the image to ImageView. Therefore, we willAsynctaskThe three generic parameters are set to:
Asynctask<string, Void, bitmap>

That is, the parameter type is String--url, the progress type--void not required, and the return type--bitmap the downloaded image. When setting the image, we also need to use tag to judge.Finally, as with multithreading, we create a Showimagebyasync method that callsAsynctask:
/** * Using asynctask * @param imageView * @param url */public void Showimagebyasync (ImageView imageView, String url) {
   asyncdownloadimage task = new Asyncdownloadimage (imageView, url);    Task.execute (URL);}

in asynctask There are several places that you need to be aware of:
    • The Execute method can only be called in the UI thread
    • Asynctask Instances must also be created in the UI thread
    • Asynctask Abstract methods do not call manually, the system will automatically manage
    • Asynctask can only be performed once
Finally, only the Mimageloader.showimagebythread (Viewholder.imageview, url) of the GetView method in adapter is required, as shown below:
Mimageloader.showimagebyasync (Viewholder.imageview, URL);
Finally, the display of course is no problem, it does not map.


my github
My video mu-class network




Android asynchronous load full parsing using 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.