Android-handle detailed introduction and face questions

Source: Internet
Author: User
Tags message queue

Android-handle

What's a handle?
Handle:
A Handler allows you to send and process Message and Runnable objects associated with a thread ' s MessageQueue
A handle is an object that allows you to go and send and receive messages to and from a thread's message queue

You can construct a handle object to communicate with Looper in order to push a new message into the MessageQueue, or receive a message from the Looper (removed from message queue)
Such as:

Handle Features:
Handle instances are associated with message processing, sending and receiving to match
Handle can only be attached to handlethread threads
Handle can select its attached thread by setting the Looper
Handle all operations are using a thread
The Removemessage function of handle only removes the message from the queue

Two effects:
1, perform scheduled Tasks
The main functions used are:
Postattime,postdelayed,sendmessageattime,sendmessagedelayed

2, inter-thread communication
When the Android app starts, a main thread is created, and the main thread creates a message queue to process the various messages
When you create a sub-thread, you can get the handle object created in the thread in your child thread.
Because Android requires the interface to be updated in the UI, the most common use is to update the UI of the main thread in the child threads

3. Ensure that the operation always runs in a particular thread
For example, when we load data from a database, except when the program starts, it needs to load the external
We also need to reload every time we receive notification of data changes in order to determine the validity of the data (always using the data from the last query)
and reduce unnecessary query operations, we should make sure that they run in the same thread

4, the concept used:
Thread: The UI thread is usually the main thread, and the Android launcher will create a message Queue for him

Message: Messages

MessageQueue: Message Queuing, used to hold messages placed by threads

Looper: Equivalent to a tunnel, a thread can produce a Looper object, he manages the MessageQueue message queue inside the county.
Looper.mylooper directly Looper the current thread
Looper.getmainlooper getting the looper of the UI main thread

The implementation principle of Looper:
A message mechanism similar to Windows programming
Receive Message Queuing messages in Android and distribute them to handle,
Distribute to window in Windows

MYTH: Handle not necessarily in the main thread

three-side questions:
1, when multiple handle use the same handlethread, can there be more than one handle in the same handlethread?
Yes

2, when multiple handle use the same handlethread, will a message with multiple handle in the same handlethread be confused? Why?
No, because each message has a corresponding target
Either post or SendMessage, and finally call Sendmessageattime, in the same thread, in the same queue
Attach handle source code, easy to view and analyze
Http://pan.baidu.com/s/1bnEuvU3

Can the 3,handle be created into any thread?
No, you can only create handle and send messages in looper.onlooperprepared

4, how to specify the thread that handle runs
You can specify the thread to run as long as you specify the looper used by handle, which increases the flexibility of the program

5, will different messages in the same handle be executed at the same time?
It is impossible to cross-execute, only to process a message to process the next message

6, for the message being executed, what will happen to call Removemessage, and will the message terminate?
No, once the message is sent out, Removemessage is invalid, removemessage can only
To remove a message from a queue

For example, the user Quick Click button, my program can not be fast enough corresponding, what should I do?
Call Removemessage first to remove the same action

7, using handle is asynchronous, will it create a new thread?
Will not be built, just run in a new thread

is 8,handle within the main thread?
Not necessarily

Does the 9,handle post and SendMessage use a queue or two?
Use the same one

Copyright NOTICE: Welcome to Exchange, QQ872785786

Android-handle detailed introduction and face questions

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.