What is Handler used for?
1 Execution of Scheduled tasks, you can perform certain tasks at the scheduled time, you can simulate the timer
2) communication between threads. When the Android application starts, a main thread is created, and the main thread creates a message queue to handle the various messages. When you create a child thread, you can get the handler object created in the parent thread in your child thread, and you can send a message through the object to the parent thread's message queue. Because Android requires updating the interface in the UI thread, you can update the interface in other threads by this method.
For performance tuning, the Android UI is not thread-safe, which means that if multiple threads concurrently manipulate the UI components, it can cause thread-safety issues. To solve this problem, Android has a simple rule that allows only UI threads to modify the UI components in the activity.
When a program starts for the first time, Android also initiates a main thread (main thread), which is primarily responsible for handling UI-related events, such as user key events, user contact screen events, and screen drawing events, and distributing related events to the corresponding components for processing. So the main thread is often called the UI thread.
The Android messaging mechanism is another form of "event handling", which is primarily designed to address the multi-threaded problems of Android applications--android platform only allows UI threads to modify the UI components in the activity, This causes the newly started thread to be unable to dynamically change the property values of the interface component. However, in the actual development of Android applications, especially in the game development involving animation, it is necessary to allow the newly-started thread to periodically change the attribute value of the interface component, which requires the help of the handler message passing mechanism.
Introduction to the Handler class
The main functions of the handler class are two:
1. Send a message in a newly initiated thread.
2, in the main thread to get, processing messages.
In order for the main thread to handle the messages sent by the newly launched thread "in a timely manner", it is obvious that it can only be done by a callback--the developer simply rewrites the method of handling the message in the Handler class, and when the newly-launched thread sends the message, the message is sent to the MessageQueue Handler will constantly get and process messages from the MessageQueue-this will result in a callback to the method that handles the message in the Handler class.
The handler class contains the following methods for sending and processing messages.
1. void handlemessage (Message msg): A method for handling messages. This method is typically used to be overridden.
2, Final Boolean hasmessages (int what): Check message queue for the inclusion of the What property to specify a worthwhile message.
3. Final Boolean hsamessages (int what,object object): Check Message Queuing for messages that contain the What property to a specified value and the object property to the specified object.
4. Multiple overloaded message obtainmessage (): Gets messages.
5, Sendemptymessage (int what): Send empty message.
6, Final Boolean sendemptymessagedelayed (int what,long delaymills): Specifies how many milliseconds to send empty messages.
7. Final Boolean SendMessage (msg): Send a message now.
8. Final Boolean sendmessagedelayed (Message Msg,long delaymillis): Specifies how many milliseconds to send messages.
Using the above methods, the program can easily use the handler class for message delivery.
Working principle of Handler, Loop and MessageQueue
To better understand how handler works, let's first introduce several components that work with handler.
1. Message objects accepted and processed by Message:handler
2, Looper: Each thread can only have one looper. Its loop method is responsible for reading the message in MessageQueue, and then handing the message to the handler that sent the message after reading the message.
3, MessageQueue: Message Queuing, it uses the FIFO method to manage messages. When a program creates a Looper object, it creates a Looper object in its constructor. The constructor source code provided by Looper is as follows:
Private Looper ()
{
mqueue=new MessageQueue ();
Mrun=true;
Mthread=thread.currentthread ();
}
The constructor uses a private adornment, indicating that the programmer cannot create the Looper object through the constructor. It is easy to see from the code above that the program creates a MessageQueue associated with the looper when it initializes, and the MessageQueue is responsible for managing the message.
1, Handler: Its function has two--sends the message and processing the message, the program uses Handler to send the message, the message which Handler sends must be sent to the specified MessageQueue. That is, if you want handler to work correctly, you must have a MessageQueue in the current thread, or the message will not be saved MessageQueue. However, MessageQueue is managed by Looper, which means that if you want handler to work properly, you must have a Looper object in the current thread, and you can do this in two cases to ensure that there are looper objects in the current thread.
1, the main UI thread, the system has initialized a Looper object, so the program directly create handler can be, and then through the handler to send messages, processing messages.
2, the programmer starts the child thread by itself, the programmer must create a Looper object of its own, and start it. Create a Looper object to invoke its prepare () method.
The prepare () method guarantees that there is at most one Looper object per thread. The source code for the prepare () method is as follows:
public static final void prepare ()
{
if (Sthreadlocal.get ()!=null)
{
throw new RuntimeException ("only One looper May is createed per thread ");
}
Sthreadlocal.set (New Looper ());
}
Then call the Looper static loop () method to start it. The loop () method uses a dead loop to constantly remove messages from the MessageQueue and divide the extracted messages into the corresponding handler for processing.
To sum up, the functions of Looper, MessageQueue and Handler are as follows:
1, Looper: Only one looper per thread, it is responsible for the management of MessageQueue, will continue to remove messages from MessageQueue. And the message is divided into the corresponding handler processing.
2, MessageQueue: Looper is responsible for the management. It uses a first-in, first-out approach to managing message.
3, Handler: It can send the message to Looper management MessageQueue, and is responsible for processing looper distributed to it message.
The steps for using handler in threads are as follows:
1. The prepare () method that invokes the Looper creates the Looper object for the current thread, and when the Looper object is created, its constructor creates a matching MessageQueue.
2. After having looper, create an instance of the handler subclass, overriding the Handlermessage () method, which is responsible for handling messages from other threads.
3, call the Looper loop () method to start the Looper.
The above is a small set to introduce the Android handler message delivery mechanism, I hope to help you!