Newbies in Android face difficulty with communicating between different threads.
It is better to create a new background thread instead of delaying the UI thread when there is lengthy task.
Let us say you want to read an html file. If you read this file in UI thread, the program appears to be unresponsive. Hence it is better to read the file in a background thread.
But when this reading is completed, how do you show the html in a webview in this example? The background threads can not access the ui widgets. Hence there is a need to communicate between ui thread and background thread.
This can be done with Handlers. Handlers are used to send and process messages. A handler created in a thread, can process all the messages sent by this thread.
You should create a handler class for processing messages
private class MyHandler extends Handler{
@Override
public void handleMessage(Message msg) {
if(msg.getData().getString("message").equals("your message here")){
Toast.makeText(ShowPage.this, "your toast here...", Toast.LENGTH_LONG).show();
}
}
}
Next initialize an object of this MyHandler class in onCreate
class MyActivity extends Activity
{
MyHandler mHandler;
//more variables here
//
public void onCreate(Bundle b){
//some code here
mHandler = new MyHandler();
//
}
Let us say I create a thread to write to a file
Thread th = new Thread(){
public void run(){
//here goes my lengthy
//processing statements
------
//processing completed
Message msg = new Message();
Bundle b = new Bundle();
b.putString("message", "your message here");//key and value
msg.setData(b);
mHandler.sendMessage(msg);//inform that thread is completed
}
}//my thread ends here
At the end, I should create a message object and write the string to it. Then send this message to the handler.
Handler has handleMessage method which checks at the string and if it is matching, takes appropriate action.
That's all. Our thread with handler is ready to go.
It is better to create a new background thread instead of delaying the UI thread when there is lengthy task.
Let us say you want to read an html file. If you read this file in UI thread, the program appears to be unresponsive. Hence it is better to read the file in a background thread.
But when this reading is completed, how do you show the html in a webview in this example? The background threads can not access the ui widgets. Hence there is a need to communicate between ui thread and background thread.
This can be done with Handlers. Handlers are used to send and process messages. A handler created in a thread, can process all the messages sent by this thread.
You should create a handler class for processing messages
private class MyHandler extends Handler{
@Override
public void handleMessage(Message msg) {
if(msg.getData().getString("message").equals("your message here")){
Toast.makeText(ShowPage.this, "your toast here...", Toast.LENGTH_LONG).show();
}
}
}
Next initialize an object of this MyHandler class in onCreate
class MyActivity extends Activity
{
MyHandler mHandler;
//more variables here
//
public void onCreate(Bundle b){
//some code here
mHandler = new MyHandler();
//
}
Let us say I create a thread to write to a file
Thread th = new Thread(){
public void run(){
//here goes my lengthy
//processing statements
------
//processing completed
Message msg = new Message();
Bundle b = new Bundle();
b.putString("message", "your message here");//key and value
msg.setData(b);
mHandler.sendMessage(msg);//inform that thread is completed
}
}//my thread ends here
At the end, I should create a message object and write the string to it. Then send this message to the handler.
Handler has handleMessage method which checks at the string and if it is matching, takes appropriate action.
That's all. Our thread with handler is ready to go.
Comments
Post a Comment