Click here to Skip to main content
11,922,641 members (57,529 online)
Click here to Skip to main content
Add your own
alternative version


4 bookmarked

Executing Asynchronous Tasks using Threads and Updating the UI

, 21 Apr 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
Add asynchronous tasks, using threads, in Android activities while updating its progress to the UI. Use this technique for SDKs lower than 1.5.


Since Android SDK 1.5, there is a specific class for handling UI tasks asynchronously: AsyncTask. You can find its description and usage at Asynchronous tasks using AsyncTask. However, one may need to implement applications with API levels previous to SDK 1.5 and use asynchronous tasks. For that, one needs to implement Threads manually and also handle cases where the UI must be updated while this asynchronous task is being executed.

Using the Code

Firstly, one must be aware that UI updates are only allowed in the main thread, where the UI life-cycle methods are executed. Therefore, one cannot simply start a separate Thread from the UI main thread and update interface elements. For that, a special mechanism is required. In order to do so, one must:

  1. Start a separate Thread.
  2. Execute all expense jobs in the run() method of the started Thread.
  3. When one needs to update the UI, within the separate Thread, one needs to call the sendEmptyMessage(int) or sendMessage(Message) method of a special Handler. This Handler comes from the package android.os.Handler. Within the sendEmptyMessage(int) or sendMessage(Message) method one can, and should, update the required UI elements.

Below is the code which executes what is stated above:

package com.progressbar;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ProgressBar;
 * Activity which illustrates the use of actions executed in background.
public class MainActivity extends Activity
    private ProgressBar mProgress;
    private int mProgressStatus = 0;
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState)
        // Get the progress bar which will be the UI element updated while
        // executing another task.
        mProgress = (ProgressBar) findViewById(;
        // Start lengthy operation in a background thread
        Thread lenghtyTask = new Thread(new BackgroundThread());
    // Thread which executes the lengthy operation in a separate thread
    // and update the UI - a progress bar.
    private final class BackgroundThread implements Runnable
        public void run()
            while (mProgressStatus < 10)
                // execute hard work/expensive task and update progress
                // counter
                mProgressStatus = doWork();
                // Update the progress bar - update UI
                // One can use sendMessage(Message msg) in order to send an
                // specific message to the method where the UI will be updated.
    // handler which updates the UI (Progress Bar)
    private Handler mHandler = new Handler()
        // This method should be implemented in order to update the UI. Any data
        // that must be passed, should be put in the attribute Message msg.
        public void handleMessage(android.os.Message msg)
            if (mProgressStatus < 10)
            {// update progress bar for some time
            } else
                // stop progress bar
    // counter
    private int counter = 0;
    // method which performs hard work
    private int doWork()
        // the hard work here is stopped for 1 second and updates a counter.
        } catch (InterruptedException e)
        return counter++;

The code above executes what was stated in the steps just mentioned. The lengthy Thread is started (step 1). Within it, an expensive job is performed and the progress bar status is returned (step 2). After that, the sendEmptyMessage(int) is called. This method belongs to the handler mHandler. This handler simply updates a Progress Bar while a counter is 10. After that, the Progress bar stops (step 3).


As you can see, the mechanism for executing an asynchronous task while updating a UI is not much complicated, as long as you know how to do it correctly.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Graduated from Universidade Estadual de Campinas (State University of Campinas - UNICAMP) having experience with undergrad research.

Started working as a developer with Java and .NET Web and Desktop applications. Also, developed Eclipse PDE, Perl, PHP and Android applications.

Currently working as a Tech Lead, Architect and ScrumMaster for .NET and Java Enterprise applications.

Main interests: Software Architecture, Technical Leadership, Web and Mobile applications, SCRUM

You may also be interested in...

Comments and Discussions

QuestionNice Post!! Great Pin
boss prabu11-Jun-13 9:14
memberboss prabu11-Jun-13 9:14 
AnswerRe: Nice Post!! Great Pin
Eduardo Antonio Cecilio Fernandes12-Jun-13 2:42
professionalEduardo Antonio Cecilio Fernandes12-Jun-13 2:42 
GeneralMy vote of 4 Pin
Member 816675929-Apr-13 12:05
memberMember 816675929-Apr-13 12:05 
GeneralRe: My vote of 4 Pin
Eduardo Antonio Cecilio Fernandes6-May-13 2:43
memberEduardo Antonio Cecilio Fernandes6-May-13 2:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151125.1 | Last Updated 21 Apr 2013
Article Copyright 2013 by Eduardo Antonio Cecilio Fernandes
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid