Saturday, 21 May 2011

Android - Sending and receiving messages

One way to communicate in Android is using Messages and Handlers. The handler are who receive the message and handler it and the message is the content to send.

1. Creating the message
To create a message, several method could be used:
  • Message()
  • obtain()
  • obtain(Message orig)
  • obtain(Handler h)
  • obtain(Handler h, Runnable callback)
  • obtain(Handler h, int what)
  • obtain(Handler h, int what, Object obj)
  • obtain(Handler h, int what, int arg1, int arg2)
  • obtain(Handler h, int what, int arg1, int arg2, Object obj)
Those methods are used as constructor. The method obtain() works exactly as the constructor Message(), which return a instance of message from the general pool. The rest of the methods accept several parameters:
  • Message orig: Value to copy from another message
  • Handler h: the target (used with the method sentToTarget())
  • Runnable callback: Runnalbe that will execute when the message is handled. (See example)
  • int what: Classification used internally to classify the message. The coder can specify the value and then, once the message is handler, use the sentence "Switch" to classify it.
  • int arg1, int arg2: Extra information that could be attached with the message.
  • object obj: Specify the object that the message is carrying
2. Set the value
Once the message is created, the object to carry should be specified. Because it is the Superclass Object, it should be possible to carry everything.
To specify it, the object called "obj" in the message will be signed. For example, to set a String, the code are:

Message msg = obtain();
String message = "message";
msg.obj = message;

3. Send the message
There are two ways to send a message:
3.1. Send the message from the message.
3.1.1 Specify the target by using the method setTarget(Handler) in the message. For example:
msg.setTarget(handler)

3.1.2 Send the message by using the method sendToTarget() in the message. For example:
msg.sendToTarget()

3.2. Send the message from a handler
3.2.1 Call the method sendMessage(message) from the handler which should receive the message. For example:
handler.sendMessage(msg)

4. Receiving the message
Once the message is send, the handler should receive the message. To constructor of the handler is very simple:
Handler handler = new Handler();
It contains a method called handlerMessage(Message msg) which should be modified to handler the message. The way to do it is:

Handler handler = new Handler () {
    @Override
    public void handleMessage(Message msg) {
    //Handle the message
    }
};

5. Get the data from the message.
Once the message is received, the data should be extract first. Because the type is Object, it should be cast first. For example, to get a String from a messge:

final String msgReceived = (String) msg.obj;

More information
Official android development reference (Message): http://developer.android.com/reference/android/os/Message.html
Official android development reference (Handler): http://developer.android.com/reference/android/os/Handler.html

2 comments:

  1. Thank you very much!! , I am very confused about it.

    ReplyDelete
  2. Thank you very much

    ReplyDelete