Sunday, February 1, 2009

A first look into Android Thread

I have started looking into Android. Late to join the show? well, may be, but surely its pretty exciting.

I have tried to build a utility class that can do Network I/O. And grasping things was little hard at first. Network I/O or other heavy-duty stuff in Android should be done on other worker thread. Because doing it in main thread (or the UI thread) can ( and will ) make your application unresponsive and may be killed as the system is persuaded to think that it has hung. For every Android developer, this is a must-read.

So you have to do the long running operations in separate thread. And to interact between threads you have to resort to Handler. A Handler is used to send message or runnable to a particular thread. The thing to remember is that a Handler is associated with the MessageQueue of the single thread which has created it. After creating a Handler, it can be used to post message or runnable to that particular thread.

Here is an example



public class MyActivity extends Activity {

void startHeavyDutyStuff() {

// Here is the heavy-duty thread
Thread t = new Thread() {

public void run() {
while (true) {

mResults = doSomethingExpensive();

//Send update to the main thread
messageHandler.sendMessage(Message.obtain(messageHandler, mResults));
}

}
};
t.start();
}

// Instantiating the Handler associated with the main thread.
private Handler messageHandler = new Handler() {

@Override
public void handleMessage(Message msg) {
switch(msg.what) {
//handle update
//.....
}
}

};
}





So what is done here? Very simple. A Handler has been created which is bound to the message queue of the main thread. And by using the handler, from the heavy-duty, worker thread we send message to the main thread to be processed.

Feeling like drinking from hose-pipe? Dont worry, I'll post more realistic example soon.

15 comments:

Topher said...

Hey- I just wanted you to know that your article was really helpful when I was starting my first Android app. Thanks!

Fabian said...

Thanks, that helped me a lot!

darkknight0611 said...

Thanks a lot. It helps me very much.

Brett Kromkamp said...

Thanks for this info - it has helped me with my own Android tutorial: http://www.quesucede.com/page/show/id/conway_game_of_life_android.

Vikrant Bains said...

Thnx for nice tutorial ..

Vikrant

Jamie said...

Perfect - just what I was looking for! Thank you!

yn0t said...

Thank you! Thank you! Thank you!

I've been trying to figure out how to allow my child thread to communicate with my parent thread for a couple weeks now! All I needed was a quick example just like above!

Keerthy said...

Nice you can visit this site also http://android-codes-examples.blogspot.com/2011/03/how-to-run-runnable-thread-or-ui-thread.html

Shubham said...

Nice arrange

Android app development said...

This is one of the fantastic post.We can get useful tips by read your post.This is one of the superior Information.
Android app developers

shubham singh said...


Nice Thread Tutorial, i am new in android , its help me a lot ...

I have got some good links here at androidexample.com
Thread_With_Handlers

Software Download said...

Thanks for sharing this amazing post. It's very helpful for all Android user's.
Download Free Software's by Clicking here == >
Download Crack Software |
FREE WIFI HOTSPOT APP ANDROID Free Download

Madam Enimem said...

APK Apps Free Downloads | Chef Story App

Nancy Maule said...

Thank u so very much for posting such a wonderful Info.
download free cricket games | Power Cricket T20 Cup 2016 APK

Amisha Patel said...

Its a valuable content shared,would like to know more about it.

Hydraulic Hose Dealers Mumbai, Hose Pipes Mumbai