Overview:To facilitate communication between threads, the Handler mechanism simplifies the development of multithreading through collaboration between Handler and Looper, MessageQueue, and Message classes. The thread's interaction is encapsulated into a message, and the thread that implements the
The Android UI is thread insecure, and if you try to do UI action in a child thread, the program may crash. The solution should be to create a message object, then send it with handler, and then get the message object just sent in the handler Handlemessage () method, and then the UI action here will no longer crash.Thi
Use Android Handler asynchronous message processing mechanism to create a powerful image loading class
Recently, a group was created to facilitate communication. Group Number:55032675
The previous blog introduced the Android asynchronous Message processing mechanism. If you do not know about it, you can see: Android asynchronous
In the second article, "How touse handler to implement message distribution mechanism in Android (i)", we talked about the main thread of the Looper is the Android system in the launch of the app, it has helped us to create a good, And if you need to use handler in a child thread, we need to explicitly call Looper's prepare method and the loop method to create it
First, the message class does not need to say much, the internal use what property to hold the identifier, with obj (object) to hold the data to carry, with target (target) to hold the target handler.So there needs to be a handler (handle) class to handle the message, by calling the SendMessage (message) method in the
Objective:The previous article is about bitmap and memory optimization technology, this article to everyone talk about handler.Handler is an important knowledge of the Android system, which is often asked in Android multithreaded interviews and is often used in real-world projects. Of course, more complex, more knowledge, involved in the class has thread, Looper, Message, MessageQueue.Android is multi-threaded, usually the user-related UI events in th
Preface:The previous article is about bitmap and memory optimization technology, this article to everyone talk about handler.Handler is an important knowledge of the Android system, which is often asked in Android multithreaded interviews and is often used in real-world projects. Of course, more complex, more knowledge, involved in the class has thread, Looper, Message, MessageQueue.Android is multi-threaded, usually the user-related UI events in the
Introduction:For performance tuning purposes, Android UI operations are not thread-safe, which means that if there are multiple threads concurrently manipulating the UI component, it can cause thread safety issues.To solve this problem, Android has a simple rule: only allow the UI thread to modify the components in the acitivity, which will cause the newly-started thread to dynamically change the value of the Build property.However, in the actual application development, it is necessary to let t
public static final Looper myLooper() { return (Looper)sThreadLocal.get(); }
Here is a summary of the handler execution process:
1. logoff. Prepare () methodBind logoff to the current thread,Create a messagequeue In The logoff Constructor2. Create handler and write handlemessage3. Use handler to send messages, and the final messages will be sent to the
first, take a look at the use of the process1. Using handler instances in child threads /*********** Child Threads Use Handler instance *********/ private class Looperthread extends thread {public Handler Handler; @Override public Void Run () { looper.prepare ();
Foreword: in our daily development work, it is inevitable that the message is transmitted between threads, and the most common way of implementation of this process is the handler message mechanism. Of course, this is not our focus today, and today we are going to focus on the intermediates ofMessaging: message. How do
Asynchronous Message Processing Mechanism-Handler principle in Android (continued)
The Asynchronous Message Processing thread refers to the infinite loop that the thread enters after it starts. Each cycle is performed once, a message is taken from the internal message queue
First worship the Great God's post, from the perspective of the source of the analysis of Android handler mechanism. Link:android message processing mechanism (figure + source analysis)--looper,handler,messageHere is not to explain, just do some introduction, want to see the details please poke the above link.Android's messaging mechanism consists of three blocks
The relationship among Message, Handler, MessageQueue, and logoff in a single-thread model.Handler introduction:A Handler allows you to send and process Message and Runable objects, which are associated with the MessageQueue of a thread. Each thread instance is associated with a separate thread and the MessageQueue of
Handler1. MessageMesssge is a message passed between threads, which can carry a small amount of information inside, to exchange data between different threading, the What field of the message, and in addition to using the Arg1 and Arg2 fields to carry some integral data, using the Obj field to carry a single Object object.2.HandlerHandler the processor, it is used primarily to send and process messages. Sen
Simply put, handler gets the Looper object in the current thread, Looper is used to hold the message taken out of the MessageQueue, and the message is distributed and processed by handler, in FIFO execution.MessageQueue (Message Queuing): Used to hold a
Daily Development and Application of Android Handler message mechanism and code Testing
In many cases, we need to create and maintain a View for each component or all UI threads. For example, to update the display of a TextView
We cannot directly create sub-threads in the UI thread. We need to use the message mechanism: handl
order to avoid the ANR, you should perform long-time operations in the child threads, and after this operation is complete, it is possible to notify the main thread to modify the UI.III. Basic Principles and procedures1, the role of handler is mainly 2:(1) Send a message.(2) Get and process messages.2, the basic principle: in order to allow the main thread to deal with the
As we all know, time-consuming operations should not be performed in the main thread, such as fetching data from the server and updating the interface. However, the interface update can only be performed in the main thread. In this case, the thread gets the data from the server, and then sends the data to the main thread via handler and updates the interface in the main thread. In general, we are doing this:1 NewThread (NewRunnable () {2 @Override3
First say why you need handler:The main thread of Android is also called the UI thread, which handles UI -related time. Android only allows UI components to be modified in the main thread, and when we need to modify the UI interface in other threads, we need to send a message to the main thread, The main thread then handles the UI modification events. perform a time-consuming operation or network operation in the UI thread, or the ANR ("Application N
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.