Learn about android os handler dispatchmessage, we have the largest and most updated android os handler dispatchmessage information on alibabacloud.com
Handler
Every beginner Android development is not open handler this "ridge", why it is a ridge, first of all, this is one of the essence of Android architecture, and then most people are aware of it but do not know why. Today, see Handler.post This method decided to go over the source code to comb the implementation o
This article is mainly on the handler and message loop implementation of the principle of source analysis, if unfamiliar handler can see the blog "Detailed Android handler use method", It explains why Android introduced the handler
Android uses Handler to implement message delivery mechanism (2). androidhandler
Before starting this article, let's summarize some key points about Handler, logoff, and MessageQueue in the first two articles:
0) before creating a Handler in the thread, you must first call logoff. prepare (), create a local thread vari
In Android asynchronous processing 1: Using Thread + Handler to update the UI of a non-UI Thread.Overview: Android uses the message mechanism to implement inter-thread communication. A thread establishes its own message loop through logoff. MessageQueue is a FIFO message queue, and logoff is responsible for extracting messages from MessageQueue, and distribute it
the data sent from the Message:
// Declare a Handler object and implement the handleMessage () method private Handler handler = new Handler () {public void handleMessage (android. OS. message msg) {if (msg. what = 3) {textview. s
, it creates a new message and sets its mcallback to the runnable object, and inserts the message into messagequeue.
4) handler functions include processing messages (executing message content) and forwarding messages, which are mainly processed by handlemessage and dispatchmessage functions;
5) in handler, both send message and post runnable are added to message
cyclically reads the Message object from the MessageQueue queue, submits the Message object to Handler for processing, and calls the dispatchMessage method of Handler.
Now let's take a look at the basic implementation code of Handler:
// Create a handler in the main threadN
Now we have a problem first. We use myThreadHandler. sendEmptyMessage (0); To send a message object, how does Handler receive and process the message object? First, I will draw a data structure diagram:
From this figure, we can clearly see that after sendEmptyMessage is called, the Message object will be put into a MessageQueue queue, which belongs to a logoff object, and each logoff object passes through ThreadLocal. set (new logoff () is bound to
Android asynchronous Message processing mechanism allows you to deeply understand the relationships among logoff, Handler, and Message, and androidlogoff
Reprinted please indicate the source: http://blog.csdn.net/lmj623565791/article/details/38377229, this article from [Zhang Hongyang's blog]
Many people have been asked about the interview. What is the relationship between logoff,
(me.mlogging!= null) me.mLogging.println ("
Check again at\sdk\sources\android-16\android\os\handler.java in thedispatchmessage function, execute runnable or callhandlemessage for processing.
Process message, the method is called by Looper public void DispatchMessage (Message msg) { if (
code first.
Private Handler mHandler = new Handler () {// parameter: Message is equivalent to a carrier of information and can transmit data from sub-threads to the main thread, that is, the so-called inter-thread communication... Public void handleMessage (android. OS. Message msg) {// This method is executed in the
When Android uses Handler for instantiation (new), such as: private Handler Handler = new Handler ();
Will complain "Handler is abstract, cannot to be instantiated.", prompt use, other instantiation way.
Cause: An error occurred
submits it to handleMessage of Handler for processing. After the processing is complete, call Message. recycle () to put it into the Message Pool.
4. Handler: the Message processor. handler encapsulates the information to be transmitted into a Message, which is implemented by calling the obtainMessage () of the handler
A lot of people interviewed must have been asked, what is the relationship between Looper, Handler, and message in Android? The purpose of this blog is to introduce 3 relationships from the source point of view, and then give a conclusion that is easy to remember.1. Overview handler, Looper, and message all three concepts related to the
processing is complete, call message. Recycle () to put it into the message pool.
4. Handler:
The handler of the message handler is responsible for encapsulating the information to be passed into a message and implementing it by calling the obtainmessage () of the handler object;
Send the message to logoff, which is
Android message mechanism Handler parsing (source code + Demo)
Handler is one of the most common problems for developers During the interview. This article will fully explain Handler, including the source code layer and usage methods.
If you have any questions after reading the article, you are welcome to discuss it in
Before also because weekend night watching TI3 competition, has not found the time to write a blog, resulting in a long period of not updated. Ashamed! Back to the progress of the next, try to make sure to write every week. Here is also the first to congratulate the Alliance team from Sweden won the TI3 champion, I hope next year China team can tiger up!Getting started, we all know that the Android UI is thread insecure, and if you try to do UI action
NULL to invoke its Handlemessage method. If it is still empty, the handlemessage of Handler itself is invoked, which is the method that we rewrite when we create the Handler.
If the Handler post (Runnable R) method is invoked when a message is sent, the Runnable is encapsulated into the callback of the Message object, and then the sendmessagedelayed is invoked,
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.