Handler of Android thread communication

Source: Internet
Author: User

There are two types of Android threads:

1) system-created main thread for handling UI interface and user interaction events

2) Child threads created by themselves to implement other business

The controls in the UI are thread insecure, can manipulate controls only in the main thread, and cannot manipulate controls directly in other child threads.

If you want to manipulate controls in the main thread in other threads, you can only update the interface by sending a message telling the main thread.

To facilitate communication between threads, Android implements the handler mechanism.

Messages are passed through handler between threads. Messages are managed by a FIFO message queue (MessageQueue).

There can only be one MessageQueue per thread, and the system will create one for the main thread by default, but none of the child threads we create ourselves.

Just Looper can create a MessageQueue for our child threads.

About Looper:

1) Call Looper.prepare () to create a message queue for the thread and guarantee only one.

2) Looper.loop () is a dead loop that is constantly MessageQueue out of the messages distributed to handler.

About Handler:

1) When you create a handler object, the handler object is associated with the message queue for that thread. The main thread has a system-created message queue by default, and the child thread does not have a default, so you must first call Looper.prepare () to create a handler object before creating it in a child thread.

2) The Handlemessage (message msg) method must be overridden to process the received message.

3) Call the Handler object's SendMessage (message msg) to send a message to the message queue.

Processing message scenarios in child threads:

1 NewThread (NewRunnable () {2 3 @Override4      Public voidrun () {5                 6     //1. Call Looper.prepare () to create a message queue7 Looper.prepare ();8     //2. Create handler object override Hanlemessage () method for handling messages9Handler3 =NewHandler () {Ten  One @Override A      Public voidhandlemessage (Message msg) { -         Switch(msg.what) { -          Case3: the              Break; -       } -         Super. Handlemessage (msg); -         } +     }; -     //3. Call Looper.loop () to distribute messages from the message queue to handler; + Looper.loop (); A  at } -}). Start ();

If you need to process the message in the main thread, you only need to rewrite handlemessage () when creating the handler object, because the main thread has already created a message queue for us, so we don't need to call the methods in Looper.

Summarize:

A thread can have only one message queue (MessageQueue), one thread may create multiple handler objects, and they share a message queue for that thread.

The system creates a message queue for the main thread by default, and the child threads do not.

1) Before you create the handler object, you must call Looper.prepare () in the child thread to create a message queue for the thread, which is not required by the main path.

2) When you create a handler object, you must override the message that the Handlermessage (message msg) callback function uses to process the Looper.loop () distribution to the handler object.

3) After you create the handler object, you must call Looper.loop () in the child thread to distribute the message from the message queue to the handler object for processing, which is not required for the main thread.

Handler of Android thread communication

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.