Monday, April 20, 2015

Handler in android

Handler:

A Handler allows you to send and process Message and run-able objects associated with a thread's MessageQueue. Each Handler instance is associated with a single thread and that thread's message queue. When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it -- from that point on, it will deliver messages and run-able to that message queue and execute them as they come out of the message queue.



There are two main uses for a Handler: (1) to schedule messages and run able to be executed as some point in the future; and (2) to enquirer an action to be performed on a different thread than your own.


Example:

your main class
{

  //call your asyncTask from where you get Data..


new async(handler_object).execute();//pass Handler_object to your async_task

//Declare a Handler object in your main class.
//Handler in The UI Thread Retreieves The Data
//And Can Update the GUI as Required

 private Handler handler = new Handler() {
  @Override
  public void handleMessage(Message msg) {

Bundle bundle = msg.getData();
String data= bundle .getString("Data");

//Do anything you want with data.........

}

};



}



your_async_task
{

      Handler handler;

Async_task_constructer(Handler handler)
{
      this.handler=handler;
}


//doing in backgroud()
{

      //hit your webservice here

}


//onpost_method()
{

     //Here when you will get response...just pass all data to your main activity by using handler object

        Message msg =  new Message();
        Bundle bundle = new Bundle();

        bundle.putString("Data", "Data");
         msg.setData(bundle);

        handler.sendMessage(msg);

}
}


For example:

1) Add handler

Add an instance of Handler class to e.g., your Activity instance.

public class MyMap extends Activity {
    . . .
    public Handler _handler = new Handler() {
@Override
public void handleMessage(Message msg) {

            // This is where main activity thread receives messages
            // Put here your handling of incoming messages posted by other threads
super.handleMessage(msg);
}

};
. . . .

}

2) Post Message

In the worker thread post a message to activity main queue whenever you need Add handler class instance to your MapActivity instance.

/**
* Performs background job
*/
class MyThreadRunner implements Runnable {
// @Override
public void run() {
while (!Thread.currentThread().isInterrupted()) {
// Just dummy message -- real implementation will put some meaningful data in it
Message msg = Message.obtain();
msg.what = 999;
MyMap.this._handler.sendMessage(msg);
// Dummy code to simulate delay while working with remote server
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}



0 comments:

Post a Comment

Don't lose faith when you see others receive answers to their prayers

An elephant and a dog became pregnant at same time. Three months down the line the dog gave birth to six puppies. Six months later the dog...

 

G-Expo Template by Ipietoon Cute Blog Design