Handler created, must have a looper, the main thread has created itself. Other threads need to be created by themselves, not by default. Create a method1. This method is to create a system-defined handlerthread, which is run in a non-UI thread, has created a good looper, directly use it. Create handler have multiple constructors, see the source codeHandlerthread Threadhandler = new Handlerthread ("1111");Th
.4. What is LooperLooper is the meaning of the loop, which is responsible for taking the message out of the message queue and handing it over to the target processing5. What is the difference between a thread and a looper? If a thread does not have a looper, there is no message queue and the message cannot be processed, and handler cannot be used inside the thread. This is why creating a handler inside a ch
I. Looper1. How do I use Looper?(1) define a looperthread.Class Looperthread extends Thread {Public Handler Mhandler;public void Run () {Looper.prepare (); Call prepare ();Looper.loop (); Enter the message loop.}}(2) Use Looperthread in the application:{New Looperthread (). Start (); Start a new thread, and the thread function is run.}2. Looper.prepare () function:public static final void prepare () {if (sthreadlocal.get () = NULL) {throw new RuntimeE
For this part of the content, will be divided into 4 sections to describe:1. Responsibilities and relationships2. Message loops3. Threads and Updates4. A few summary--------------------------------------------------------------------------------------------------1) Next, we begin this part of the content, first of all to understand the respective responsibilities and the relationship between each other. DutyMessage: Messages that contain the message ID, the message Processing object, and the pro
Handler, Looper, and message all have concepts related to the Android asynchronous message processing thread. So what is the asynchronous message processing thread? When the asynchronous message processing thread starts, it enters an infinite loop body, takes a message out of its internal message queue once per loop, and then callbacks the corresponding message handler, and then resumes the loop after the completion of a message. If the message queue
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 message sent through handler, usually attached to a thread that created it, can get the message queue of the current thread through Looper.myqueue ().Handle
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 of Looper,handler,messagequeue. We know that
let's look at the source code. 1) To see what Looper.prepare has done? The Prepare () method called its overloaded method and passed in the parameter true. 1 private static void Prepare (boolean quitallowed) { 2 if ( Sthreadlocal.get ()! = null ) { 3 throw new runtimeexception (" Looper may created per thread " 4 5
Sthreadlocal.set (new Looper (quitallowed )); 6 } Sthreadlocal is T
messageWays to create a message1, new one on the line2. Using Handler.obtainmessage (...) This approach allows us to get a message instance from the public loop pool, and the efficiency will increaseSo it would be better for us to use this methodMessageQueue IntroductionMessageQueue (Message Queuing) is used to store messages in FIFO mode,Looper IntroductionLooper is called a message loop, and he constantly checks for new messages on MessageQueue, an
a summary of handler.Message: Messages, including the message ID, the message processing object and the processing of data, etc., by the MessageQueue Unified Queue, the final processing by Handler . Handler: Handler, responsible for sending and processing the message. When using Handler , you need to implement the handlemessage (message msg) method to process a specific message , such as updating the UI such as MessageQueue: Message Queuing for storing messages sent by Handler and running in ac
Looper PreparationWhen handler is instantiated, it gets looper from the current thread, obtaining MessageQueue for sending messages. Then, the thread is not born with a Looper object, it needs to call the static method Looper.prepare () in the threads execution, and eventually calls to the following static method:Private Static void Prepare (boolean quitallowed)
In the main UI thread, the system has initialized a Looper object, so the program creates handler directly, then posts handler to send messages and process messages.The program ape itself initiates the child thread, the program ape must create itself a Looper object, and start it, create the Looper object to call his prepare () method. The methodEnsure that each
Looper is driven by calling the loop method to carry out a message loop: Blocking out a message from MessageQueue, and then letting handler process the message, the loop method is a dead loop method.How do you stop the message loop? We can call the Looper quit method or the Quitsafely method, which is slightly different.Looper's Quit method source code is as follows:publicvoidquit() { mQueue.quit(false);
); Handlelaunchactivity (R, NULL); Trace.traceend (Trace.trace_tag_activity_manager); } break; ...When Applicationthread sends a launch_activity message, MH executes the code snippet above, the most important of which is to call handlelaunchactivity (R, NULL), The method calls performlaunchactivity to launch the corresponding Activity;performlaunchactivity method, which uses ClassLoader to load the class file for the activity. Then call Activity.attach (), wh
); } }; And then call it somewhere else.Handler.post (myrunnable);Handler.post (Myrunnable,time);Case VIEW: http://shaobin0604.javaeye.com/blog/515820====================================================================Knowledge Point Summary Supplement:Many novice Android or Java developers are still confused with thread, Looper, handler, and message, derived from Handlerthread, Java.util.concurrent, Task, Asynctask as the current lite
Looper plays the role of the message loop in the message loop, he will not stop to take out the messages from the MessageQueue, if there is a message to deal with, will not be blocked. Creating a looper creates a MessageQueue when the Looper is created Private Looper (Boolean quitallowed) { mqueue = new Messa
A toast was used in the test service to print the log. Prompt cannot call Looper.prepare () After adding, the code executes normally. Looper.prepare (); Toast.maketext (Getapplicationcontext (), "Services Thread", Toast.length_long). Show () Looper.loop (); But then there was another toast, and then there was an error stating that each thread could have only one looper caused by:java.lang.RuntimeException:Only one
void run () {System.out.println ("Runab LEID::::::::::: "+thread.currentthread (). GetId ()); System.out.println (" Runablename: "+thread.currentthread (). g Etname ()), try {Thread.Sleep (10000), and catch (Interruptedexception e) {22 TODO auto-generated catch block23 e.printstacktrace ();24}25}26};27} FourWe pass the data through a message and finally print out the data we passed in the Handlemessage () method.What bundles are:Bundle is a special map, which is a tool for passi
, stating that they shared a queue.Actually looper by taking a message from the message queue and then judging by the priority from highThe type of message, there are three types of (1) message inside of the callback, an object that implements the Runnable interface, where the run function does the processing work;2) handler inside the Mcallback point to a realization of the callback interface object, by its handlemessage processing;3) Processing the
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.