What does zero mean in Handler.sendEmptyMessage (0)

I am learning Android and I am stuck in this statement. From Google:

Sends a message containing only that value.

Returns

Returns true if the message was successfully placed in the message queue. Returns false on failure, usually because looper processing of the message queue ends.

Someone please explain to me what a message containing zero will do. Thanks

+6
source share
1 answer

This means what . what is basically an integer that allows the recipient to identify received messages.

Your handleMessage function is as follows

 public void handleMessage (Message msg) 

a Message object is passed to you, and you can check the public what field to find out what the message is. ( msg.what )

Eg.

you send two types of messages: what value is 1 for success and 0 for failure

so your handleMessage function will look something like this.

 public void handleMessage (Message msg) { switch (msg.what) { case 1: //success handling break; case 0: //failure handling break; } } 

Now you can have sendEmptyMessage(0) for success and sendEmptyMessage(1) for failure.

Remember that you do not need to send an empty message, you can send a Message object with a lot of data connected to it.

for example, to send a message with some success text you can do

 Message.obtain(mHandler, 0, "Success text") 

and likewise for failure

Now, according to what zero means, it just sends an empty message, and 0 can be replaced with any value. The idea is that you have only one type of message, and the handler understands this. Therefore, there is no need to check which message he received, just need to receive a message. So sendEmptyMessage(AnyInteger) will work fine. 0 by agreement only

+14
source

All Articles