Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does zero mean in Handler.sendEmptyMessage(0)

Tags:

android

I am learning Android and I am stuck at this statement. From google:

Sends a Message containing only the what value.

Returns

Returns true if the message was successfully placed in to the message queue. Returns false on failure, usually because the looper processing the message queue is exiting.

Someone please explains for me what the Message containing zero will do. Thanks

like image 474
IMPENTA Avatar asked Mar 11 '13 12:03

IMPENTA


People also ask

What is the handler in Android?

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 a Looper .

What is handler and looper in Android?

Looper is an abstraction over event loop (infinite loop which drains queue with events) and Handler is an abstraction to put/remove events into/from queue with events (which is drained by Looper) and handle these events when they are processed.

Does handler run on UI thread?

Android handles all the UI operations and input events from one single thread which is known as called the Main or UI thread. Android collects all events in this thread in a queue and processes this queue with an instance of the Looper class.

How do you use kotlin handler?

To execute the handler in the current thread, retrieve the Looper of the current thread by using the Looper. myLooper method. Therefore, you can push the Runnable in the local thread's message queue for later processing.


1 Answers

It means the what value. what is basically an integer that allows the receiver to identify the messages it receives.

Your handleMessage function looks like this

public void handleMessage (Message msg)

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

Eg.

you send two types of messages, with what value 1 for success and 0 for failure

so your handleMessage function would 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 dont have to send an empty message, you can send a Message object with more data attached to it too

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

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

and similarly for failure

Now as per what the zero means, It is just sending an empty message and the 0 could be replaced by any value. The idea is in this case you have only one type of messages and the Handler does understands that. So it does not need to check what kind of message it has received it just needs to receive a message. So sendEmptyMessage(AnyInteger) would work fine. 0 is just by convention

like image 195
Ahmed Aeon Axan Avatar answered Oct 18 '22 01:10

Ahmed Aeon Axan