Getting Started in Android Development Part 43 - The AsyncTask Class

North Border Software




Home Tutorials Hints & Tips About
Share on Facebook Share on Twitter

Getting Started in Android Development

Part 43 - The AsyncTask Class

This the next two tutorials should be looked at together. They look at the AsyncTask and the URLConnection classes. In this tutorial we focus on building a framework for the AsyncTask and in the next part we complete the URLconnection and test the AsyncTask. The URL connection class replaces the HttpClient class that was deprecated in API level 22. Its purpose is to allow Internet connectivity to download data such as JSON or XML files.

The AsyncTask Class

Up until this time we have performed all actions on the main User Interface thread. A thread is a piece of processing that is managed independently. If you look back to the JSON and XML tutorials, the data handled was small and accessed locally from the device. You can envisage a situation if that data was a large file and accessed over the Internet. If this was handed on the main user interface, then that the interface would soon become unresponsive to button clicks whilst the data was being downloaded. Ultimately, the app may crash. The AsyncTask allows us to create a new processing thread that has its own scheduling and resources such that this data can be downloaded in the background separate to the main User Interface such that the responsiveness of the app is unaffected.

Our Tutorial Example

In this tutorial we will simply download an image using the AsyncTask from the Internet and display that image in an ImageView. The download will start on a button click. The XML code for this simple layout is shown below:

Within the onClick method for the button we only need one line of code to start the AsyncTask.

new ourAsyncTask().execute(stringUrl);

We will need to define "ourAsyncTask" by building a private subclass within the AsyncTask Activity but outside of the onCreate() method. You will notice that we pass a string parameter. This is defined as a constant and is the URL of an image file on my website that will be downloaded.

stringUrl = "http://www.northborder-software.com/images/asynctask_test.jpg";

We then need to define the private ourAsyncTask subclass

private class ourAsyncTask extends AsyncTask<String, Integer, Bitmap> {
}

You will notice that we need to extend the ourAsyncTask class with AsyncTask and define three object types. In our example they are <String, Integer, Bitmap>. Generically, these are: Params, Progress and Result. The relevance is that we will pass a String into the AsyncTask. The Integer is typical of the type of parameter used whilst the task is being performed. We may use an Integer whist the task is running to update something like a progress bar. We will use a progress dialogue and so we could use the void keyword as it requires no Integer parameter. Finally, we will return and Image file or Bitmap as the result.

The class ourAsyncTask must override at least one method. This has to be at least the doInBackground method. We will also override the onPreExecute and onPostExecute methods. We may want to also use the onProgressUpdate method, but this is not needed for our example. The resultant ourAsyncTask class framework is shown below:

private class ourAsyncTask extends AsyncTask<String, Integer, Bitmap> {

   @Override
   protected void onPostExecute(Bitmap result) {
      super.onPostExecute(result);
   }

   @Override
   protected void onPreExecute() {
      super.onPreExecute();
   }

   @Override
   protected Bitmap doInBackground(String... Params) {
      return null;
   }
}

The bulk of the work will be done by the doInBackground method. However, this will be covered in the next tutorial. All we need to do for the onPreExecute method is define and start a progress dialog box with the following line of code:

waitDialog = ProgressDialog.show(AsyncTaskActivity.this, "Please wait", "Downloading Image");

It only requires a context (AsyncTaskActivity.this), a text string for an instruction to the user and a text string to inform the user what it is doing.

The onPostExecute method can be completed with two lines of code to set the downloaded bitmap to the ImageView and dismiss the progress dialog.

downloadedImage.setImageBitmap(result);
waitDialog.dismiss();

In the next tutorial we will look at the URLconnection class and complete the doInBackground method.

After this tutorial your AsyncTaskActivity.java file should look similar to the one below:

Your MainActivity.java file should look similar to the one below:

Your Android Manifest file should look similar to the one below:


Download Download tutorial set 3

© 2015 North Border Software All rights reserved
Privacy and Cookies Terms of Use Disclaimer Copyright
North Border Software Logo

Android Apps and Training