Do we need threads in android? Yes. Especially when you have long running operations like network access. These operations will block the main thread, which runs the user interface (UI), and have the effect that the UI is not able to receive user input. The following example simulates the problem:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView) findViewById(R.id.textView);
textView.setText("hello World");
// simulate long running operation
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
Log.e(TAG, "Thread.sleep", e);
}
}
Encapsulate processes which might take longer in threads. Particularly when the process is started in the lifecycle of the activity (onCreate, onResume, onPause).
Besides that you may receive an “Application Not Responding (ANR) dialog” from android if the activity is not responding.
So we create a thread which prints a message after finishing it’s work:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView) findViewById(R.id.textView);
textView.setText("hello World");
thread = new Thread() {
public void run() {
try {
// simulate long running operation
Thread.sleep(5000);
textView.setText("new value...");
} catch (InterruptedException e) {
Log.e(TAG, "run in thread", e);
}
}
};
thread.start();
}
All set? Not yet. See how you app beautifully dies with a CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 
Just like in Java or C# an UI object (textView in our case) can only be modified by the thread which created it. Which is in our case the main UI thread (not the new thread).
See also the articel Painless Threading on the android developers page. This tutorial here shows how to update the UI from a thread using a Handler.
A handler instance in an activity is bound to the main UI thread. And is able to update UI objects when receiving notifications from other threads.
Next we subclass the Handler class and override the handleMessage method. msg.obj contains the message from the other thread.
private Handler uiHandler = new UIHandler();
class UIHandler extends Handler {
@Override
public void handleMessage(Message msg) {
// a message is received; update UI text view
textView.setText(msg.obj.toString());
super.handleMessage(msg);
}
}
Create the message object in the long running thread and pass it to the handler. Use Message.obtain() to take a message from a pool of recycled objects:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView) findViewById(R.id.textView);
textView.setText("hello World");
thread = new Thread() {
public void run() {
try {
// simulate long running operation
Thread.sleep(5000);
// create message which will be send to handler
Message msg = Message.obtain(uiHandler);
msg.obj = "new value...";
uiHandler.sendMessage(msg);
} catch (InterruptedException e) {
Log.e(TAG, "run in thread", e);
}
}
};
thread.start();
}
The uiHandler object receives the message and prints the content in the UI object.
You have different threads and want to control where the message comes from? Use the msg.what to identify the message:
class UIHandler extends Handler {
private static final int ID_0 = 0;
private static final int ID_1 = 1;
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case ID_0:
// a message is received; update UI text view
if (msg.obj != null)
textView.setText(msg.obj.toString());
break;
default:
break;
}
super.handleMessage(msg);
}
}
Change the thread to send a message with the user defined code:
// create message which will be send to handler
Message msg = Message.obtain(uiHandler, UIHandler.ID_0);
msg.obj = "new value...";
uiHandler.sendMessage(msg);