Handler Message delivery mechanism

Source: Internet
Author: User

The concept of 1,handler
What is Handler used for?
1) Perform scheduled tasks, perform certain tasks at scheduled times, and can simulate timers

2) Inter-thread communication. When the Android app starts, a main thread is created, and the main thread creates a
Message Queuing to process various messages. When you create a child thread, you can get the parent thread in your child thread
Creates a handler object that can be used to send messages to the parent thread's message queue. Because Android
Requires that the interface be updated in the UI thread, so that the interface can be updated in other threads by this method.

Role Description:
1) Looper: A thread can produce a Looper object that manages this line thread the message Queue

2) Handler: You can construct Handler objects to communicate with Looper in order to push new messages into the message queue, or
Receives Looper messages sent from the message Queue.

3) Message queue (Message Queuing): Is the message used to hold the thread into place.

4) Thread: The UI thread is usually the main thread, and the Android launcher will create a message Queue for it.

Each thread can contain a Looper object and a Message Queue data structure. In your application,
You can define Handler subcategories to receive messages sent by Looper.

2,handler the execution of the process

Each handler corresponds to a thread, and the message sent handler the child thread into the message queue, which is then distributed to handler processing by Looper.


3,handler Asynchronous Implementation method

See Example

http://download.csdn.net/detail/fulinwsuafcie/4437306


The relationship between 4,handler and threads

Handler generally runs on the main line range

Problem:
1, using handler is asynchronous, will it create a new thread? No
Is 2,handler in the mainline range? Typically within the main thread, but you can also create handler in a child thread
3,handler the Post and SendMessage methods, using a queue of uneasiness is two? One
4, create a handler in the sub-thread, and then SendMessage what happens? Will crash
5, the child thread establishes handler, constructs the time comfortable to enter the main thread the Looper? Yes

If you are interested, you can look at the original Android comments, excerpted from the Handler.java code header comments.

/**
* A Handler allows you to send and process {@link Message} and Runnable
* Objects associated with a thread ' s {@link MessageQueue}. Each Handler
* instance is associated with a single thread and that thread ' s message
* Queue. When you create a new Handler, it's bound to the thread/
* Message queue of the thread that's creating it-from this point on,
* It'll deliver messages and runnables to that message queue and execute
* Them as they come out of the message queue.
*
* <p>there is both main uses for a Handler: (1) to schedule messages and
* Runnables to being executed as some point on the future; and (2) to Enqueue
* An action to being performed on a different thread than your own.
*
* <p>scheduling messages is accomplished with the
* {@link #post}, {@link #postAtTime (Runnable, Long)},
* {@link #postDelayed}, {@link #sendEmptyMessage},
* {@link #sendMessage}, {@link #sendMessageAtTime}, and
* {@link #sendMessageDelayed} methods. The <em>post</em> versions allow
* You to enqueue Runnable objects to being called by the message queue when
* they is received; The <em>sendMessage</em> versions allow Enqueue
* A {@link Message} object containing a bundle of data that would be
* Processed by the Handler's {@link #handleMessage} method (requiring that
* You implement a subclass of Handler).
*
* <p>when posting or sending to a Handler, can either
* Allow the item to be processed as soon as the message queue are ready
* To does so, or specify a delay before it gets processed or absolute time for
* It to be processed. The latter-implement timeouts,
* Ticks, and other timing-based behavior.
*
* <p>when A
* process is created for your application, it main thread is dedicated to
* Running a message queue that takes care of managing the top-level
* Application objects (activities, broadcast receivers, etc) and any Windows
* They create. You can create your own threads, and communicate back with
* The main application thread through a Handler. This is do by calling
* The same <em>post</em> or <em>sendMessage</em> methods as before, but from
* Your new thread. The given Runnable or Message would then be scheduled
* In the Handler ' s message queue and processed when appropriate.
*/

Transferred from: http://blog.csdn.net/fulinwsuafcie/article/details/7760041

Handler Message delivery mechanism

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.