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

Source: Internet
Author: User

The collation of a post:

Handler must be instantiated in the main thread? 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

In general, if your handler is to refresh the operating UI, you need to run under the main thread.

Case

1. To refresh the Ui,handler to use the looper of the main thread. Then in the main thread Handler Handler = new Handler (), if this function is to be satisfied in other threads, then Handler Handler = new Handler (Looper.getmainlooper ());

2. Do not refresh the UI, just process messages. Handler Handler = new Handler () if the current thread is the main path, Looper.prepare (); Handler Handler = new Handler (); Looper.loop (), or handler handler = new Handler (Looper.getmainlooper ());

If you instantiate it, use Looper.getmainlooper () to put it on the main UI thread to handle it.

If not, because only the UI thread defaults to Loop.prepare (); Loop.loop (); too, other threads need to call these two manually, otherwise they will get an error.

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

What is commonly used to distinguish messages, such as when you pass in msg.what = 3;

Then deal with the time to judge Msg.what = = 3 is not set up, is the words, said the message is what to do (you can distinguish open)

As for ARG1,ARG2, it is actually two data, two int value, see what you want to do with it. If your data is just a simple int value, then using these two, it is more convenient.

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

Msg.obj, this is the transmission of data, MSG can carry objects, in the Handlemessage, you can take this data out to do processing. However, if it is the same process, it is best to use the above SetData, this is generally the Messenger class to be used to pass the serializable object across the process, this is more than the above, more consumption of performance.

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

Two Good articles:

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

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

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

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.