initializes a Looper object when creating the UI thread, and also creates a MessageQueue associated with it;
Handler: The function is to send and process information, if you want Handler to work properly, there is a Looper object in the current thread
Message: Handler
thread is not the same thread as the activity thread on which it resides. Handler Use example 4This example will learn how to start a thread without using runnable, but instead use the Handlerthread looper to construct a handler and then handler to get the message, pass the data, and process the message yourself. This
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 threadNormalHandler = new
Descriptive narrative of the problemOnce upon a time, we used the original method to use handler will have the following a warm hint:classstaticor leaks might occurHere's a more specific description (warning on Android Studio, not knowing if Eclipse is the same)Since This Handler is declared as a inner class, it may prevent the outer class from being garbage collected. If the
There are many and thorough descriptions of this topic on the Internet, such:
Http://www.cnblogs.com/xirihanlin/archive/2011/04/11/2012746.html
Http://disanji.net/2011/01/23/android-looper-handler-thread/
Http://zhoujianghai.iteye.com/blog/1097156
In the process of using handler, the following should be the most:
Mhandler. sendmessage (MSG,...); (1)
Mhandler. Po
The concept of 1,handlerWhat is Handler used for?1) Perform scheduled tasks, perform certain tasks at scheduled times, and can simulate timers2) Inter-thread communication. When the Android app starts, a main thread is created, and the main thread creates aMessage Queuing to process various messages. When you create a child thread, you can get the parent thread in your child threadCreates a handler object t
); Timer.schedule (task, 10000); } }Method Five: (Runnable + handler.postdelayed (runnable,time))Regularly update the UI on Android, usually using java.util.Timer, java.util.TimerTask, Android. Os. Handler combination. In fact, handler itself has provided a timed function. Private New Handler (); Private New Runnable () {public void run
override this method to accept data
@ Override
Public void handleMessage (Message msg ){
// TODO Auto-generated method stub
Log. d ("MyHandler", "handleMessage ......");
Super. handleMessage (msg );
// The UI can be updated here
Bundle B = msg. getData ();
String color = B. getString ("color ");
MyHandlerActivity. this. button. append (color );
}
}
Class MyThread implements Runnable {
Public void run (){
Try {
Thread. sleep (10000 );
} Catch (InterruptedException e ){
// TODO Auto-generated cat
enable the message loop. (Except for the main thread, the main thread system automatically creates a logoff object for it to enable message loop .)The logoff object stores messages and events through MessageQueue. A thread can have only one logoff corresponding to one MessageQueue.
(2) The Handler object is usually used to interact with logoff. Handler can be seen as an interface of logoff to send messages
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
()));}};
...// Send the message to hanlderMyThreadHandler. sendEmptyMessage (0 );
Now you know the process from sendEmptyMessage to handleMessage. the MessageQueue queue is transferred to the thread where the Looper is located for processing. This is an asynchronous process. Of course, the thread where the Looper is located can also be the thread where the sendEmptyMessage is located.
After reading the abo
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 message sent by the child threads in a timely manner, it is obvious that the c
.
Returns:
Returns true if the Runnable is successfully placed in to the message queue. Returns false on failure, usually because the looper processing the ' message ' queue is exiting.
We can pass the Runnable object (usually runnable subclass) by calling the handler post method, and handler will invoke the Looper run
Handler Looper poll MessageQueue Message Object1The main thread is called when it is created, Public Static void Preparemainlooper() {} constructor method. Public Static void Preparemainlooper() {Prepare (false);synchronized(Looper.class) {if(Smainlooper! =NULL) {Throw NewIllegalStateException ("The main Looper has already been prepared."); } Smainlooper =
();
14.//The Looper obtained by Handlerthread is passed to the handler object, that is, the message queue is processed by the looper in the other thread instead of the default binding when handler is initialized.
//Handlerthread as the name implies is the thread that can handle the message loop, which is
is the relationship between message, logoff, handler, messagequeue, and other places ???
8. What is the difference between sendmessage and post ?? Do you want to share a Message Queue messagequeue ?? (They use the same handlerthread;
The difference between sendmessage and post is that there is a difference between dispatchmessage () in the loop () function of logoff.
public static final void loop() { Loope
Handler is primarily the main thread and sub-threading communication. In general, some time-consuming operations in a child thread are done to notify the main thread to modify the UI.In fact, the Android system in activity startup or state changes are implemented through the handler mechanism.First, enter into the main method of Activitythreadpublic static void Main (string[] args) { ... Loope
/w3cnote/android-tutorial-handler-message.html) Third, the implementation flowchart of Handler(Image source: http://www.runoob.com/w3cnote/android-tutorial-handler-message.html) Flowchart Analysis: related nouns
UI Thread : is our main thread, the system initializes a Looper object when creating the UI thr
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.