Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do Android Message 'what' codes need to be unique in the scope of a Handler or a thread?

Is the Message 'what' field unique to a Handler instance or to the MessageQueue of the thread that one or more Handler instances may be associated with? The docs read "A Handler allows you to send and process Message and Runnable 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 runnables to that message queue and execute them as they come out of the message queue" which seems to suggest that Message 'what' codes must be unique to a MessageQueue and therefore within the scope of a single thread rather than a single instance of Handler.

I have several Handler objects associated with my UI thread throughout my project, so do I have to ensure that all Message 'what' codes that are sent to any of these Handlers are unique, or do they only have to be unique for each Handler instance?

For example, what would the logs say after the following code is executed and both messages have been received:

class MainActivity extends Activity{
  public static final String TAG = "MainActivity";
  public static final int FIRST_PURPOSE_MESSAGE_CODE = 1;
  public static final int SECOND_PURPOSE_MESSAGE_CODE = 1;
  ...
  private static class FirstUIThreadHandler extends Handler{
    ...
    @Override
    public void handleMessage(Message msg){
      switch(msg.what){
        case FIRST_PURPOSE_MESSAGE_CODE:
          Log.v(TAG,"handling the first message");
          break;
      }
    }
  }
  ...
  private static class SecondUIThreadHandler extends Handler{
    ...
    @Override
    public void handleMessage(Message msg){
      switch(msg.what){
        case SECOND_PURPOSE_MESSAGE_CODE:
          Log.v(TAG,"handling the second message");
          break;
      }
    }
  }

  ...
  FirstUIThreadHandler firstUIThreadHandler = new FirstUIThreadHandler(...);
  SecondUIThreadHandler secondUIThreadHandler = new SecondUIThreadHandler(...);
  Message firstMsg = firstUIThreadHandler.obtainMessage();
  firstMsg.what = FIRST_PURPOSE_MESSAGE_CODE;
  firstUIThreadHandler.sendMessage(firstMsg);
  Message secondMsg = secondUIThreadHandler.obtainMessage();
  secondMsg.what = SECOND_PURPOSE_MESSAGE_CODE;
  secondUIThreadHandler.sendMessage(secondMsg);
  ...

}
like image 836
CCJ Avatar asked Oct 21 '22 01:10

CCJ


1 Answers

The messages will be delivered only to the handler you use to send the message, two different handlers will not share any info about the messages sent to a different handler, hence, if you have the same "what" attribute in different messages, it will be passed to the "handleMessage(Message msg)" of the handler that called "sendMessage(yourMessage)", for example:

secondUIThreadHandler.sendMessage(secondMsg);

In the code above it means that the message will be received by the second handler only and it will be able to use the "what" attribute no matter if that attribute is the same for another message other than the current one used in secondUIThreadHandler.

Hope it helps!

Regards!

like image 181
Martin Cazares Avatar answered Oct 28 '22 23:10

Martin Cazares