In the constructor method, the Looper object is obtained by calling Looper.mylooper (). If Mlooper is empty, an exception is thrown: "Can" t create handler inside thread that has not called looper.prepare () "means that you cannot invoke Looper.prepare ( Creates a handler for the thread. The example above does not call this method, but it does not throw an exception. In fact, because the main thread has been invoked by us at startup, we can crea
A few key concepts1, MessageQueue: is a data structure, see the name of righteousness, is a message queue, the place to store messages. Each thread can have a maximum of one MessageQueue data structure.When a thread is created, its MessageQueue is not created automatically. A Looper object is typically used to manage the MessageQueue of the thread. When the main thread is created, a default Looper object is
The message processing mechanism in Android consists of four parts: message, Handler, MessageQueue, and Looper, and MessageQueue encapsulated in Looper, we generally do not deal directly with MQ.First, LooperLooper literally means "circulator", which is designed to make a normal thread into a looper thread . The so-called Loo
This paper first analyzes the whole Android messaging mechanism from the whole architecture, and then introduces the functions and tasks of each component from the source point of view. The basic concepts are not introduced, and the mechanism of threadlacal and garbage collection should be researched by ourselves.Infrastructure ArchitectureFirst, we need to look at the overall architecture to see what the Android thread communication does. As we all know, a process is the smallest unit of resour
This article includes the following issues related to handler:(1) The role of handler(2) Why is Android designed to update the UI only in the UI thread?(3) Two exceptions to Handler(4) The relationship between Handler and Looper MessageQueue (source angle)(5) Thread-related handler, i.e. handlerthread (source angle analysis)(6) The main thread sends messages to the child threadFirst, the role of handler:
(1) To complete the time-consuming ope
I. OverviewThe Android asynchronous message processing mechanism consists of four parts, message, Handle, MessageQueue, and Looper. I'll give you a brief introduction to these four sections below.1.MessageA message is an information that is passed between threads, and it can carry a small number of messages inside to exchange data between different threading.2.MessageQueueMessageQueue is a message queue that is primarily used to store all messages sen
http://xilinx.eetrend.com/article/7471With the advent of the NI pxie-5644r vector signal transceiver (VST), ni reshaped the concept of the instrument by introducing the flexibility of the user-programmable FPGA into the RF instrument.1. High performance and revolutionary designNI pxie-5644r VST combines typical RF I/O functions in a vector signal analyzer (VSA) and vector signal Generator (VSG) with NI or u
The Looper class in Android is a class used to encapsulate message loops and message queues for message processing in Android threads. Handler can actually be seen as a tool class for inserting messages into message queues.(1) The Looper class is used to open a message loop for a thread. By default, the newly born thread in Android does not have a message loop turned on. (except for the main thread, the mai
Thread communication, Activitythread, and thread classes are key to understanding Android threading management.threads, as the basic unit of CPU scheduling resources, have a very important and fundamental role in the operating system for embedded devices such as Android. This section is mainly from the following three aspects of analysis:
"Android thread management-thread communication"
"Android Threading Management--activitythread"
"Android Threading Management--thread"
I
The basic use of hanlder can be found in the blog "Android handler use", if you want to understand handler, Looper, thread and other relationships and internal implementation principles can be see the blog "Deep source analysis of Android handler , Message,messagequeue,looper ".The description of Handlerthread in the API in Android is:Handy class for starting a new thread, which has a
相关源码framework/base/core/java/andorid/os/Handler.javaframework/base/core/java/andorid/os/Looper.javaframework/base/core/java/andorid/os/Message.javaframework/base/core/java/andorid/os/MessageQueue.javalibcore/luni/src/main/java/java/lang/ThreadLocal.java
Why do I need a messaging mechanism?First we know that there are two particularly important mechanisms in Android, one is binder and the other is the message mechanism (Handler + Looper + Mess
We all know that in the main thread (that is, the UI thread) to send a message, only need to create a handler, but you try to create a handler in the child thread, and then send a message, you will find that the program is the following exception, "can ' t create handler inside Thread that have not called looper.prepare (); "New Thread (New Runnable () {@Overridepublic void run () {Handler Mhandler = new Handler (); mhandler.sendemptymessage (0);}}). Start ();Maybe we all have questions here, so
(), but Android recommends that we pass Message.obtain () or handler.obtainmessage () Gets the message object. This is not necessarily a straightforward creation of a new instance, but rather a message instance that is immediately available from the pool of messages, and is then fetched and returned to this instance. Conversely, if there is no message instance available in the pool, a new message object is given according to the parameter. By analyzing the source, it is known that the Android s
An asynchronous callback mechanism is provided in Android handler, which allows us to make a notification after a long task has been completedHandler Basic use:In the main thread, using handler is simple, new a handler object implements its Handlemessage method, in HandlemessageProvide the corresponding processing method after receiving the message, here does not handler the use of detailed explanation, before looking at Ben Boven, the reader should first grasp the basic use of handler, I here m
An asynchronous callback mechanism is provided in Android handler, which allows us to make a notification after a long task has been completedHandler Basic use:In the main thread, using handler is simple, new a handler object implements its Handlemessage method, in HandlemessageProvide the corresponding processing method after receiving the message, here does not handler the use of detailed explanation, before looking at Ben Boven, the reader should first grasp the basic use of handler, I here m
error message calledfromwrongthreadexception. When you encounter such an exception, you should know what's going on!
2.2 Message Queue
In order to solve a similar problem in a single-threaded model, Android designed a message queue that allows information to be exchanged between threads through the messaging queue and with handler and Looper components. They are described separately in the following sections:
1. MessageMessage messages, understood
("Looper", E.getmessage (), e);
return false;
}
return Enqueuemessage (Queue, MSG, uptimemillis);
Okay, let's take a look at post ():
Public Final Boolean post (Runnable R)
{return
sendmessagedelayed (Getpostmessage (R), 0); The Getpostmessage method is the difference between two sending messages
}
The method is only one sentence, the internal implementation and the ordinary SendMessage is the same, but on
Handler is the core of the entire message system, is handler sent to MessageQueue message, and finally Looper is to inform handler, so from the handler.First, HandlerThere are many handler constructors, but they are essentially the same:Public Handler () {This (null, FALSE); } Public Handler (Callback Callback, Boolean async) { //Auto bind when the Looper mlooper = Looper.
In our common system, programs work usually in both event-driven and message-driven ways, and in Android, Java applications work by message-driven.The message-driven principle is:1. There is a message queue where messages can be delivered to this queue;2. There is a message loop that continuously extracts messages from the message queue and then processes them.The message loop is encapsulated in Android via Looper, while a Message Queuing MessageQueue
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.