Android Handler Examples with Multithreading and the Handler Class

android handler examplesThe first version of the Android system introduced some groundbreaking technology. The hardware that is being used to run Android on smartphones and other devices is steadily getting more powerful, which has allowed Google to create a more powerful operating system and do more with every passing upgrade. The apps that are being made for Android keep getting better as well. Android will continue to remain the most popular mobile platform in the world for the foreseeable future, according to experts. If you want to learn how to develop apps for Android, now is the time. This introductory Android development course can help you get started. You will need a strong base in Java (and preferably other programming languages, like C++ and XML) to develop good applications.  If you’re new to Java, you may want to first take this course on Java essentials for Android.

In this tutorial, we’re going to give you an overview of handlers and the handler class, which lets you add the multithreading functionality to your applications.

The Concept of Multithreading

Multithreading wasn’t something that Android supported initially. It was only with the introduction of Android 3.0 that the multithreading capability was introduced to the platform.

So what is multithreading exactly? It’s the ability of the operating system to run different parts of a program simultaneously. These processes do not interfere with each other (in a well written program or application) and each is referred to as a thread. Think of it as making a sandwich: you cut the bread and meats side by side and then place them on slices of bread. These processes don’t interfere with each other – in fact, they complement each other, and they are all part of the same program, which is making a sandwich.

In Android, multithreading is implemented through the Handler class. Multithreading uses your system resources efficiently to get a task done. It also allows the operating system to juggle several tasks at the same time.

The Handler Class

The handler class lets you process and send message objects, which contain descriptions and data, and runnable objects that refer to a MessageQueue belonging to a thread. A handler will let you execute message objects and runnable objects at a specified time in the future. Each instance of a handler will belong to a single thread only when declared. Runnable objects are those objects that contain commands that will be executed to obtain results in your program.

To explain it to you plainly, a handler be used to execute a thread. It will also allow various threads that make up a program to communicate with each other to prevent conflicts.

It’s worth your time to learn how to write programs or applications that are capable of multithreading. It will make your app run faster and better, which can only benefit you in the long run. You can sign up for this Android development course to learn this technique, among other valuable techniques.

A handler class can be implemented through the default constructor, like this:

Handler handlerObject = new Handler();

It can also be implemented through using a parameterized constrcutor, like this:

Handler handleObject = new Handler(Runnable runnableObject, Handler.Callback callbackObject);

Limitations of Multithreading in Android: Multithreading Safety

The Android UI is not multithreading safe. What does that mean? You need to understanding the way an application is run by the Android operating system to understand the concept.

Whenever an application is first started, a single thread will hold the runnable components of the application. This thread (the main thread) is generally used to handle events that occur in the application and display them to the user. It also handles user interaction, like when the user makes a change via the user interface.

Now, if you want to carry out a new task, it will invariably be assigned to the main thread. If the task assigned is resource intensive and time consuming, the application’s UI gets locked- because, as we mentioned before, the main thread controls it. Until the task is completed, the user is unable to interact with the application.

In Android, the best programming practice is to create multiple threads to handle time intensive tasks. These threads should not have the ability to interfere with the UI display. Any changes to the UI display should come through the main, and you should enable the other threads to communicate with the main thread to effect these changes. You can learn more about it in this Android course.

Example of the Handler Class

We can display the running of the handler class with a button object that you can create with an XML file. The code for the XML file is as follows:

<RelativeLayout xmlns:android=""
tools:context=".ThreadExampleActivity" >
android:text="@string/Example of Handler Class in Android " />
android:text="@string/button_text" />

This code will create a button approximately in the middle of your graphical display. You can name the button by changing the string resource. If you need help on the basics of writing an Android app, check out this course.

After this, we’ll create the code that actually implements the Handler class:

package com.nkm.thread;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Button;
import android.widget.TextView;
Public class HandlerExample extends Activity {
Handler examhand = new Handler () ;
Button mybutton;
Textview mytextview;Runnable run = new Runnable ()
public void run()
//link to button click

We have given a partial program here which you can complete. The most important part of the program is the runnable object creation, which can call to the other methods for multithreading. If you’d like help on writing your own Android programs, check out this course.

Common Methods a Handler Class used for Multithreading

Here are some methods a handler class can implement for multithreading:

  • public void dispatchMessage (Message msg) : System messages here
  • public Handler (): This will associate a handler instance with the looper for the thread. The thread must contain a looper for this to work. A looper is the class that is used to provide the message loop functionality to a thread.
  • public Handler (Looper nameofyourlooper): You can use another looper, instead of the default looper, by specifying it with this method.
  • public Handler (Looper nameofyourlooper, Handler.Callback nameofyourcallback): This method, in addition to letting you assign a new looper, will let you use a callback interface to handle messages.
  • public final boolean post (Runnable r): This will cause the Runnable object r to the be added to the MessageQueue. The handler this method is implemented in will decide which thread the runnable object goes to.
  • public final void removeCallbacks (Runnable r): Any pending callbacks in the MessageQueue can be removed with this method.

For a list of all the methods a handler class can let you implement, you can go check out the official Android documentation here. We recommend that you familiarize yourself with most of the methods (especially the methods that deal callbacks) to understand multithreading. You will probably need to use a number of these methods while creating your application.

The methods aren’t as complex as they look. You do, however, need to have a solid understanding of Java to use them. If  you need help at any time, this course can help you learn about using Java with Android 4.0.