Showing posts with label Thread. Show all posts
Showing posts with label Thread. Show all posts

Thursday, March 19, 2009

Timer in Android ( The Better Way )

The usual Timer class is available in Android, but unless you are doing non-trivial stuff, its better to use Handler for scheduling task. Why? Well, Timer class introduces a new thread and while developing mobile application, you should really think twice (or more than that!) before using it.

So, how jobs can be scheduled using Handler class? There are two different approaches. First, You can ask it to send messages at some later time by using sendMessageAtTime(Message, long) or sendMessageDelayed(Message, long) , which then can be processed as shown in my previous post, effectively acting as a timer. Secondly, you can directly ask it to run some scheduled task by using postAtTime(Runnable, long) and postDelayed(Runnable, long).

The methods described here lets you schedule a single shot task. To repeat a scheduled task, you have to register it again to run for the next time. So, assuming that you want to repeat a task after one second delay, we can do something like this,


private Handler handler = new Handler();


private Runnable runnable = new Runnable() {

public void run() {

doStuff();

/*
* Now register it for running next time
*/


handler.postDelayed(this, 1000);
}


};

To stop a repeated task you can use removeCallbacks(Runnable r), which removes all pending Runnable r in the message queue.

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.