Do Android messages need "what" codes to be unique within the scope of a handler or stream?

Is the what field unique to a thread's Handler or MessageQueue instance with which one or more Handler instances can be associated? docs read "Handler allows you to send and process Message and Runnable objects associated with a MessageQueue thread. Each Handler instance is associated with one thread and this thread's message queue. When you create a new handler, it is tied to the thread / message queue of the thread that creates it - from this point it will deliver messages and runnables to this message queue and execute them as they leave the message queue "which seems to indicate that messages" what "codes should be unique to the MessageQueue and therefore within one thread, not just one Handler instance.

I have multiple Handler objects associated with my UI thread throughout my project, so should I ensure that all the "what" codes that are sent to any of these handlers are unique or must they be unique for each handler instance?

For example, what the logs say after executing the following code 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);
  ...

}

      

+3


source to share


1 answer


Messages will only be delivered to the handler that you use to send the message, two different handlers will not pass information about messages sent to another handler, therefore if you have the same "which" attribute in different messages, it will be passed to " handleMessage (Message msg) "handler called" sendMessage (yourMessage) ", for example:

secondUIThreadHandler.sendMessage(secondMsg);

      

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



Hope it helps!

Hello!

+3


source







All Articles