Differences between obtainmessge and new message in handler in Android

Source: Internet
Author: User

Obtainmessage () is used from the message pool. One MSG does not need to be opened up. New

New requires a new application, which is less efficient and can be recycled by obtianmessage;

// Use handler. obtainmessage (), instead of MSG = new message (); // because if there is already an message object, that not be used by // any one, the system will hand use that object, so you don't have to // create and object and allocate memory. // It is also another example of object reconfiguring and reusing in Android. message MSG = mhandler. obtainmessage (); MSG. what = update_listview; MSG. OBJ = Current + "/" + total + "Songs"; // this method is called from worker thread, so we cannot update UI from here. MSG. sendtotarget ();

See the followingCode:

Message MSG = handler. obtainmessage (); MSG. arg1 = I; MSG. sendtotarget (); message MSG = new message (); MSG. arg1 = I; handler. sendmessage (MSG );

The first method is to get the message from the handler class, so that you can directly send a message to the handler object. The second method is to directly call the handler's message sending method to send a message.

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.