Sunday, July 11, 2010

Android Handler

Message Handler
I have started looking into Android.

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

  1. 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)); }}};

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

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


Anonymous said...

top [url=]free casino games[/url] coincide the latest [url=]casino bonus[/url] manumitted no set aside hand-out at the best [url=]casino

Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...

[url=]sac longchamp[/url] Leather is an expensive material and one is always careful and cautious about keeping a favorite leather jacket or other garment away from accidental damage. But, sometimes, some kind of damage can occur, in spite of your best efforts, and in such situations, it is important that you know, how you can set things right, rather than just moaning or lamenting your Reliable Store For Mulberry Aston Natural Leather Messenger Bag Black for Men,Cheap Mulberry Bags On Sale, Mulberry Bags Outlet, fast delivery. misfortune. Some methods of repairing damage such as cuts, burns or holes in leather are discussed below for your reference..
[url=]sac longchamp[/url] What a wonderful way to look at people, all people. One thing Mulberry Bags Has many Different Styles And Colors, Shop Favorite Mulberry Zip Printed Leather Purse Light Coffee for Women supply cheap I have discovered in life is that all people are precious. In fact you have never looked into the eyes of any human being, it doesn't matter what your impression of that person was, that wasn't precious in God's sight..
[url=]sacs longchamp pas cher[/url] The Ivanka Handbag has very comfortable leather handles that could be used over the shoulder make it a pleasure to carry! It has double Compartment with magnetic snap closures. Another handbag which is in rage among working women is Luca Women's Handbag. It features two main sections divided by a central zipped compartment for your valuables and internal leather pockets.. The refurbished market has a lot to offer to you and all at affordable rates. Be it any top brands of designer handbags, they are all their under the refurbished roof. Lower rates and the afresh look, without any signs of the previous user, it can be a good deal. This innovative feature works very realistically. Of course, being virtual, there is no physical coupon or ticket for the customer to scratch. Instead, the Adorable Mulberry Women's Smaller Bayswater Printed Leather Shoulder Black Bag with big discount "scratching" is done using a virtual dime.