Pugetworks
Seattle Software Developers

blog-archive

Pugetworks Blog Archive!

Android Thread Handling

Almost every Android application has two parts.  On one side is the user interface that the user controls and views the information with.  On the other side is a network component for talking to a server or updating its information.  When you write an Android application you are always given control of the thread that lets you change the user interface.  But, this bit of software processing is only for updating the user interface.  Sure you can use it to make network calls but, this is a big "no no".  The problem is that the Android application needs to be able to use the UI Thread to make updates, so if you send it off on an errand to retrieve some data from a server, then the application has no way to update the controls.  So essentially the party stops and waits until the network call is complete.  From a users perspective this is awful.  They pushed a button, and now... nothing.  Then suddenly everything is back.

In my experience its find to start creating the application this way but, before you can release it you will need to split the network calls into a separate thread.

This article is about doing just that.

Step 1.  Isolate your network calls with a thread call.

Its assumed at this point that you already have an Android app that is making network calls.  So first go through your code and surround all of them with a block of code that looks like this.

Essentially change this

makeNetworkCall();

into this

new Thread(new Runnable() {
   @Override
   public void run() {
      makeNetworkCall();
   }
}).start();

When you do this, what you are is spinning off a new thread to run out and do the waiting work.  The new problem is, what happens when this new thread is done?

Step 2. The Handler

Behind the scenes is a Java class call the Handler.

This little class can be used build a sort of callback in your main UI Thread so the new threads will have some way to notify it when they are done.  To use it add this bit of code to your Activity class.

private Handler handler = new Handler() {
   @Override
   public void handleMessage(Message msg) {
      updateTheUI(); //This method is for whatever needs to happen after the network call is complete
   }
};

Now anytime you want to actually make a call back to your UI Thread you will simply have to make this call.

handler.sendEmptyMessage(0);

That is the basics but, it leaves a big question.  What if I want to send more information than just update?

Step 3. Smarter Messages

If you want to send a better message than just update, you need to figure out how many types of messages you want and what they should be.  They have to be integer values so it would make the most sense to create a bunch of constants in the class to represent these calls.  For instance...

private static final int REDRAW = 42;

You will then need to modify your handler to look for this particular code.

private Handler handler = new Handler() {
   @Override
   public void handleMessage(Message msg) {
      if(msg.what == REDRAW) {
         updateTheUI(); //This method is for whatever needs to happen after the network call is         	
      }
   }
};

Finally, to call this special method you will need to modify your handler calling code to look like this.

handler.sendEmptyMessage(REDRAW);

In conclusion

Use a handler when you want to spin off new threads too keep the UI Experience responsive.  If you have lots of threads going, then create some custom callback messages.  This will make all of your apps feel instantly responsive.