Android Threading: Part 1

The UI layer of all Android applications runs on a single thread called the UI thread.

Performing long-running operations such as network access or database queries on this thread will block the user interface, and eventually crash your application with an Application Not Responding (ANR) error.

In this tutorial I’ll show you a simple way to perform a long-running task in the background using the AsyncTask class to download an image.

To start, create a new application and update the main.xml layout file with this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    android:orientation="vertical" >

        android:text="Tap to Download Image" />

        android:layout_below="@+id/helloText" />


When a user taps the button, we’ll:

  1. Display a busy dialog
  2. Invoke an AsyncTask to download an image from the internet
  3. Display the downloaded image in the ImageView control
  4. Hide the busy dialog

Here’s the Activity class to achieve this (note the comments):

package com.justinschultz.asynctaskexample;


import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

public class AsyncTaskExampleActivity extends Activity {
	ImageView _imgView = null;
	ProgressDialog _busyDialog = null;

	/** Called when the activity is first created. */
	public void onCreate(Bundle savedInstanceState) {
		// Need an Activity-level reference to the ImageView
		_imgView = (ImageView) findViewById(;

	public void downloadImage(View v) {
		// Need to show the busy dialog here, since we can't do it in
		// DownloadImageTask's doInBackground() method
		new DownloadImageTask()

	private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
		protected Bitmap doInBackground(String... url) {
			// You might be tempted to display the busy dialog here, but you
			// CAN'T update the UI from this method!
			return loadImageFromNetwork(url[0]);

		protected void onPostExecute(Bitmap result) {
			// Our long-running process is done, and we can safely access the UI thread

	private Bitmap loadImageFromNetwork(String url) {
		Bitmap bitmap = null;

		try {
			bitmap = BitmapFactory.decodeStream((InputStream) new URL(url).getContent());
		} catch (Exception e) {

		return bitmap;

	public void showBusyDialog() {
		_busyDialog =, "", "Downloading Image...", true);

	public void dismissBusyDialog() {
		if (_busyDialog != null) {

The AsyncTask class must be subclassed, and it’s 3 generic types are:

  1. Params: the type of the parameters sent to the task upon execution.
  2. Progress: the type of the progress units published during the background computation.
  3. Result: the type of the result of the background computation.

Not all types are always used by an asynchronous task. To mark a type as unused, simply use the type Void:

private class MyTask extends AsyncTask<Void, Void, Void> { ... }

I don’t recommend using AsyncTask for things like long-running API calls, but it’s fine for short tasks within the activity life (between onResume and onPause). I’ll cover approaches to long-running API calls in a future post.

Download the complete code for this example here.

About this entry