Although messagequeue provides direct read/write function interfaces, it generally does not directly read/write message queues to programmers. The handler get type is exactly handler.
/** * Run the message queue in this thread. Be sure to call * {@link #quit()} to end the loop. */ public static final void loop() { Looper me = myLooper(); MessageQueue queue = me.mQueue; while (true) { Message msg = queue.next(); // might block //if (!me.mRun) { // break; //} if (msg != null) { if (msg.target == null) { // No target is a magic identifier for the quit message. return; } if (me.mLogging!= null) me.mLogging.println( ">>>>> Dispatching to " + msg.target + " " + msg.callback + ": " + msg.what ); msg.target.dispatchMessage(msg); if (me.mLogging!= null) me.mLogging.println( "<<<<< Finished to " + msg.target + " " + msg.callback); msg.recycle(); } } }Generally, the handler class is used to send messages to the message queue, and the handlemessage () function of the handler class is reloaded to add the Message Processing code.
The handler object can only be added to a thread with a message queue; otherwise, an exception occurs. The following code is the constructor of the handler class:
/** * Default constructor associates this handler with the queue for the * current thread. * * If there isn‘t one, this handler won‘t be able to receive messages. */ public Handler() { if (FIND_POTENTIAL_LEAKS) { final Class<? extends Handler> klass = getClass(); if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) && (klass.getModifiers() & Modifier.STATIC) == 0) { Log.w(TAG, "The following Handler class should be static or leaks might occur: " + klass.getCanonicalName()); } } mLooper = Looper.myLooper(); if (mLooper == null) { throw new RuntimeException( "Can‘t create handler inside thread that has not called Looper.prepare()"); } mQueue = mLooper.mQueue; mCallback = null; }Note the following code:
mLooper = Looper.myLooper(); if (mLooper == null) { throw new RuntimeException( "Can‘t create handler inside thread that has not called Looper.prepare()"); }From the code above, we can conclude that before constructing a handler object, you must have executed logoff. Prepare (), but prepare () cannot be executed twice. The following is the code for lorule. Prepare:
/** Initialize the current thread as a looper. * This gives you a chance to create handlers that then reference * this looper, before actually starting the loop. Be sure to call * {@link #loop()} after calling this method, and end it by calling * {@link #quit()}. */ public static final void prepare() { if (sThreadLocal.get() != null) { throw new RuntimeException("Only one Looper may be created per thread"); } sThreadLocal.set(new Looper()); }The handler object can be created before or after the logoff. Loop () function is executed.
In previous application development, Handler objects are generally added to the initialization code of the activity. In fact, before the activity object is constructed, the thread of the activity has executed logoff. prepare () function. For details, see the following link.
Http://blog.csdn.net/manoel/article/details/39499747
A thread can contain multiple handler objects. In the logoff. Loop () function, different messages correspond to different handler objects, and different handlemessage () functions are called back.
[Kernel Research] processor _ Handler