Android Message and obtainMessage are different. androidobtain

Source: Internet
Author: User

Android Message and obtainMessage are different. androidobtain
A few days ago, I needed to implement an Ethernet function and read the source code of Ethernet. When I saw the source code in message processing, I found some differences: When I am processing messages: 1. Create a Handler object: private Handler handler = new Handler () {public void handleMessage (Message msg) {switch (msg. what) {case 0: break; default: break ;}}; 2. Message Processing: private void TestHandlerMessage () {Message msg = new Message (); msg. what = 1; handler. sendMessage (msg);} but I think other people's source code is like this: 1. First create the Handler object: private Handler handler = new Handler () {Public void handleMessage (Message msg) {switch (msg. what) {case 0: break; default: break ;}}; 2. Message Processing: private void obtainMessageTest () {Message msg = handler. obtainMessage (); msg. what = 1; handler. sendMessage (msg);} through comparison, we will find that the two methods for obtaining the Message instance are different, so I read the source code, and it is really different: view the obtainMessage () source code: figure 1: obtain method: Figure 2: objtain method: Figure 3: Message method: Figure 4: Summary: the comment of the obtain method in Figure 3 above makes it clear that a new Message instance is returned from the entire Messge pool. In this case, because it can avoid allocating new objects. If this person is the object, you can call the obtainMessage method to obtain the Message object to avoid object creation, thus reducing the memory overhead.
What is the role of HandlerobtainMessage () in android development?

Message android. OS. Handler. obtainMessage (int what, int arg1, int arg2, Object obj)

Public final Message obtainMessage (int what, int arg1, int arg2, Object obj)
Since: API Level 1
Same as obtainMessage (), counter t that it also sets the what, obj, arg1, and arg2 values on the returned Message.

Parameters
What Value to assign to the returned Message. what field.
Arg1 Value to assign to the returned Message. arg1 field.
Arg2 Value to assign to the returned Message. arg2 field.
Obj Value to assign to the returned Message. obj field.

Returns
A Message from the global message pool.

What is the difference between sendEmptyMessage and sendMessage in android Handler?

The source code is called based on different parameters.
Public final boolean sendMessage (Message msg)
{
Return sendMessageDelayed (msg, 0 );
}

/**
* Sends a Message containing only the what value.
*
* @ Return Returns true if the message was successfully placed in to
* Message queue. Returns false on failure, usually because
* Logoff processing the message queue is exiting.
*/
Public final boolean sendEmptyMessage (int what)
{
Return sendEmptyMessageDelayed (what, 0 );
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.