Handler must the main thread be instantiated? The difference between New Handler () and New Handler (Looper.getmainlooper ())

Source: Internet
Author: User
Tags prepare

The collation of a post:

Handler must the main thread be instantiated? The difference between New Handler () and New Handler (Looper.getmainlooper ())
If you instantiate without parameters: Handler Handler = new Handler (), then this will default to the current thread's Looper
Generally speaking, if your handler is to refresh the operating UI, then you need to run under the main thread.
Situation
1. To refresh the Ui,handler to use the main thread of the Looper. Then in the main thread Handler Handler = new Handler (), if in other threads, also to satisfy this function, to Handler Handler = new Handler (Looper.getmainlooper ());
2. Do not refresh the UI, just process the message. The current thread, if it is the primary thread, Handler Handler = new Handler (), not the main thread, Looper.prepare (); Handler Handler = new Handler (); Looper.loop () or handler handler = new Handler (Looper.getmainlooper ());
If instantiated, use Looper.getmainlooper () to represent the main UI thread to handle.
If not, because only the UI thread defaults to Loop.prepare (); Loop.loop (), other threads need to call these two manually, or they will complain.

Message.what,message.arg1,message.arg2,message.obj, what's the difference between them?

What is commonly used to distinguish between messages, such as when you pass in msg.what = 3;
Then the processing time to judge Msg.what = = 3 is not set up, is the case, said the news is what to do (oneself can distinguish)


As for arg1,arg2, in fact, that is, two data to pass, two int value, see what you want to do with it. If your data is just a simple int value, then use these two, more convenient.


In fact, here you have less to say, SetData (Bundle), the above two Arg is to pass a simple int, this is to pass complex data.


Msg.obj, this is the transfer of data, MSG can carry objects, in the Handlemessage, you can take this data out to do processing. However, if it's the same process, it's best to use the above SetData, which is typically used by messenger classes to pass serializable objects across processes, which consumes more performance than the above.

Http://www.cnblogs.com/xpxpxp2046/archive/2012/04/13.html



Two good posts:

Http://www.cnblogs.com/xpxpxp2046/archive/2012/04/13/2445395.html

Http://www.cnblogs.com/xpxpxp2046/archive/2012/04/13/2445355.html


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.