Android asynchronous loading full parsing using AsyncTask

Source: Internet
Author: User

Android asynchronous loading full parsing using AsyncTask
Android asynchronous loading full parsing using AsyncTask Overview Since multithreading is mentioned above, we have to mention the thread pool. Through the thread pool, we can not only manage concurrent threads, they can also improve their execution efficiency and optimize the entire App. Of course, we can create a thread pool by ourselves, but this is annoying. It still takes a lot of trouble to create an efficient thread pool. However, android provides me with a class like AsyncTask to help us quickly implement multi-threaded development. Its underlying implementation is actually a thread pool.
AsyncTask: AsyncTask, as its name implies, is used for asynchronous processing. With AsyncTask, we can easily solve the problem of multithreading and communication between multithreading. The AsyncTask parameter abstract class AsyncTask has three generic parameters:

AsyncTask 
 

Params: This generic type specifies the type of parameters that we pass to the asynchronous task for execution. SS: this generic type specifies the type Result of the parameter that our asynchronous task returns the execution progress to the UI thread during execution: the type of the result returned to the UI thread after the asynchronous task is executed. We need to specify these three parameters when creating AsyncTask.
The AsyncTask method AsyncTask has the following Abstract METHODS:
OnPreExecute (): This method is executed before the asynchronous task is executed, and is executed in the UI Thread. Generally, we initialize some UI controls in this method, for example, ProgressDialog doInBackground (Params... params): After the onPreExecute () method is executed, this method will be executed immediately. This method is used to process asynchronous tasks, the Android operating system starts a worker thread in the background thread pool to execute this method. Therefore, this method is executed in the worker thread, after this method is executed, we can send our execution results to our last onPostExecute method. Therefore, doInBackground is the only method in the four methods that must be overwritten, in this method, we can obtain data from the network and perform some time-consuming operations such as onProgressUpdate (Progess... Values): This method is also executed in the UI Thread. During asynchronous task execution, we sometimes need to return the execution progress to our UI interface, for example, downloading a network image, we need to display the download progress at any time, so we can use this method to update our progress. Before calling this method, we need to call a publishProgress (Progress) method in the doInBackground method to pass our Progress to the onProgressUpdate method at all times to update the Progress onPostExecute (Result... result): After an asynchronous task is executed, the result is returned to this method, which is also called in the UI Thread, we can display the returned results in the UI control. AsyncTask is now back to our example. We use AsyncTask to download the image. Of course, the getBitmapFromUrl () method is used to download the image. First, we will write an ASyncDownloadImage class to inherit AsyncTask:
private class ASyncDownloadImage extends AsyncTask
 
   {    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);        }    }}
 

In this AsyncTask, We overwrite doInBackground to asynchronously download images, and overwrite onPostExecute to set images to ImageView. Therefore, we set the three generic parameters of AsyncTask:
AsyncTask
 

That is, the parameter type is String -- url, and the Progress type -- Void is not required. The returned type is the image downloaded by Bitmap. When setting an image, we also need to identify it by tag. Finally, we create a showImageByASync method like multithreading to call AsyncTask:
/** * Using ASyncTask * @param imageView * @param url */public void showImageByASync(ImageView imageView, String url) {    ASyncDownloadImage task = new ASyncDownloadImage(imageView, url);    task.execute(url);}

Note the following points in AsyncTask: The execute method can only be used to call the AsyncTask instance in the UI thread, and the AsyncTask abstract method must be created in the UI thread. Do not manually call it, the system automatically manages AsyncTask and can only execute the last time. You only need to set the mImageLoader of the getView method in the Adapter. showImageByThread (viewHolder. imageView, url); as shown below:
mImageLoader.showImageByASync(viewHolder.imageView, url);
Finally, the display is of course no problem and will not be mapped.

 




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.