This tutorial describes the usage of Threads and Handlers in your application. It also covers how to handle the application lifecycle together with threads.
Threads
When an application is launched, the system creates a thread of execution for the application, called "main".
This thread is very important because it is in charge of dispatching events to the appropriate user interface widgets, including drawing events. It is also the thread in which your application interacts with components from the Android UI toolkit (components from the android.widget and android.view packages). As such, the main thread is also sometimes called the UI thread.
The system does not create a separate thread for each instance of a component. All components that run in the same process are instantiated in the UI thread, and system calls to each component are dispatched from that thread
When your app performs intensive work in response to user interaction, this single thread model can yield poor performance unless you implement your application properly. Specifically, if everything is happening in the UI thread, performing long operations such as network access or database queries will block the whole UI. When the thread is blocked, no events can be dispatched, including drawing events. From the user's perspective, the application appears to hang.
Even worse, if the UI thread is blocked for more than a few seconds (about 5 seconds currently) the user is presented with the infamous "application not responding" (ANR) dialog. The user might then decide to quit your application and uninstall it if they are unhappy.
Additionally, the Andoid UI toolkit is not thread-safe. So, you must not manipulate your UI from a worker thread - you must do all manipulation to your user interface from the UI thread.
Handler
A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue. Each Handler instance is associated with a single thread and that thread's message queue. When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it - from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue.
There are two main uses for a Handler: (1) to schedule messages and runnables to be executed as some point in the future; and (2) to enqueue an action to be performed on a different thread than your own.
When a process is created for your application, its main thread is dedicated to running a message queue that takes care of managing the top-level application objects (activities, broadcast receivers, etc) and any windows they create. You can create your own threads, and communicate back with the main application thread through a Handler. This is done by calling the same post or sendMessage methods as before, but from your new thread. The given Runnable or Message will then be scheduled in the Handler's message queue and processed when appropriate.
In this article we will use first scenario.
Tutorial: Threads & Handler
In this example we use the Handler class to handle a ProgressDialog and ImageView in a background Thread.
We will display a ProgressDialog with the message "Image Downloading" until the Thread finishes its work of image downloading. After downloading the image, if the image is successfully downloaded, we will show it, otherwise we will show an "Error" like image on the screen.
Step 1
Create a new Android project called "Handler" with the following statistics.
Project Build Target: Android 2.3 or higher
Package Name: com.test
Activity Name: HandlerActivity
Step 2
Open your "AandroidManifest" file and add the following code to it.
We need to use the permission "Internet". So add the following line:
<uses-permission android:name=
"android.permission.INTERNET"/>
Now, your manifest file will look like this.
AndroidManifest.xml