Recently interviewed for some Android developers, in addition to basic activity life cycle and other basic issues, I will generally ask the following two questions:
(1) What is the difference between service and thread?
(2) What is the difference between New Handler () in Activity and new Handler () in the thread you created?
The first question is actually a pseudo-command, because service is one of the four components of Android, and thread is just a Java-provided tool class that encapsulates thread management, whether it's activity or service, you can create a worker thread from thread, But many novices don't know the difference between them, so it's tempting to see if the interviewer knows exactly what the Android service does. The answer to this question can be found in my article, "Android Development Practice: Using Service or Thread."
The second issue, which involves the knowledge point that Android development must master: Handler, this article begins with this question and says my understanding of Handler.
When the Android app launches, a main thread (main thread) is created by default. After the main thread is started, the UI is drawn first, then it enters a message loop (loop), waits and executes various messages and events from the system, various user click/touch events, message events sent by other threads, and so on. This is a common pattern of threading, which is to enter a "Wait command", "Execute command/message", "Wait for command/message" loop.
So how do other non-UI threads interact with the main thread that enters the message loop? It depends on the handler.
Handler is an interface that the Android system provides for working threads that can interact with the outside world, and through the SendMessage () method provided by handler, the outside world can send various message events to the worker thread. Handler completes the binding to the specified thread through the constructor, whose constructor is defined as follows:
Public Handler () {This (null, FALSE);} Public Handler (Looper Looper) {This (Looper, NULL, FALSE);} Public Handler (Looper Looper, Callback Callback) {This (Looper, Callback, false);} Public interface Callback {public boolean handlemessage (Message msg);
Among them, Looper is the thread internally responsible for the implementation of the message loop object, the normal Java.thread thread inside is not such a message loop object, Android specifically provides handlerthread encapsulation of such a thread with the message loop mechanism. Handler binds to the thread's Looper object by binding it with the threads.
Callback is the callback interface for messages that are received internally by the worker thread, and when other threads send messages to the worker thread through the handler SendMessage, the worker thread notifies the listener of the received message through callback.
Note: By default, if new Handler () is not passed in to a thread's Looper object (or passed in null), the system is bound to the thread that created the Handler () object by default.
So, now you can answer the second question, what's the difference between New Handler () in Activity and new Handler () in the thread you created?
Answer:
Activiy is working in the main thread by default, so after new Handler () in activity, the Handler object is bound to the Looper object of the main thread by default. So the handler.sendmessage message is sent to the main thread, and the Handlemessage () callback obtained by passing in the callback object is also working in the main thread, This is why you can update the UI by using the following methods in the activity without causing the ANR:
New Handler (New Handler.callback () {@Override public boolean handlemessage (Message msg) {UpdateUI (); return false; }});
Similarly, if new Handler () is in the custom thread, by default the Handler () binds the thread's Looper object, So the handler.sendmessage message is sent to this thread, and the Handlemessage () callback that is obtained by passing in the callback object is also working on this thread, so in this case the Handlemessage () The UI update operation is not allowed in the function, otherwise it will cause the ANR.
At this point, the answer is clear, but the explanation about handler is not enough, such as how the thread's looper works. In the next article, I'll use the Java thread class to implement a message loop like Looper to better display the Android message loop mechanism. This article has any question or unclear place, welcome the message or the letter [email protected] exchange.
This article is from the "Shadow Three People" blog, please be sure to keep this source http://ticktick.blog.51cto.com/823160/1564550
Android Development Practice: From New Handler ()