About the UI update mechanism and misconceptions of Android
Source: Internet
Author: User
<span id="Label3"></p><p><p></p></p><p><p>The message queue and message loop of the Android system are the main thread, and other backend services cannot be updated directly, and the update UI must be executed sequentially by the message loop of the main thread;</p></p><p><p>At the same time, for more than 5 seconds of events, such as network links, suggest new thread processing, or the system will prompt the user to terminate You.</p></p><p><p></p></p><p><p></p></p><p><p>Knowledge of Enrollment Expansion:</p></p><p><p>Both the message queue and message loop for the Android system are for a specific thread, and a thread can exist (and, of course, not exist) a message queue and a message loop (Looper). In addition to the UI thread (the main thread) in android, the worker threads created by default have no message loops and message Queues. If you want the thread to have Message Queuing and message loops and have a message-handling mechanism, you will need to call Looper.prepare () in the threads first to create the message queue, and then call Looper.loop () to enter the message loop. As shown in the following code:</p></p><p><p></p></p>Java code <ol class="dp-j"> <li><li><span class="keyword">Class Looperthread <span class="keyword">extends Thread {</span></span></li></li> <li><li>public <span class="keyword">Handler mhandler; </span></li></li> <li><li></li></li> <li><li><span class="keyword">public <span class="keyword">void run () {</span> </span></li></li> <li><li>Looper.prepare ();</li></li> <li><li></li></li> <li><li>Mhandler = <span class="keyword">new Handler () {</span></li></li> <li><li><span class="keyword">public <span class="keyword">void Handlemessage (Message msg) {</span> </span></li></li> <li><li><span class="comment">//process Incoming Messages</span> here</li></li> <li><li>}</li></li> <li><li>};</li></li> <li><li></li></li> <li><li>Looper.loop ();</li></li> <li><li>}</li></li> <li><li>}</li></li> </ol><br> <ol class="dp-j" start="1"> <li class="alt"><li class="alt"><span class="keyword">Class Looperthread <span class="keyword">extends Thread {</span></span></li></li> <li><li>public <span class="keyword">Handler mhandler; </span></li></li> <li class="alt"><li class="alt"></li></li> <li><li><span class="keyword">public <span class="keyword">void run () {</span> </span></li></li> <li class="alt"><li class="alt">Looper.prepare ();</li></li> <li><li></li></li> <li class="alt"><li class="alt">Mhandler = <span class="keyword">new Handler () {</span></li></li> <li><li><span class="keyword">public <span class="keyword">void Handlemessage (Message msg) {</span> </span></li></li> <li class="alt"><li class="alt"><span class="comment">//process Incoming Messages</span> here</li></li> <li><li>}</li></li> <li class="alt"><li class="alt">};</li></li> <li><li></li></li> <li class="alt"><li class="alt">Looper.loop ();</li></li> <li><li>}</li></li> <li class="alt"><li class="alt">}</li></li> </ol><p><p></p></p><p><p>This allows the thread to have a message-handling mechanism. If you do not call Looper.prepare () to create a message queue, it will be reported "java.lang.RuntimeException:Can ' t create handler inside thread that have not called Looper.prepare () "error.</p></p><p><p></p></p><p><p>The relationship between UI thread, Worker Thread, Handler, massage Queue, looper can be clearly displayed:</p></p><p><p></p></p><p><p>Such as: UI thread, Worker Thread, Handler, massage Queue, Relationship between looper</p></p><p><p></p></p><p><p></p></p><p><p>A few basic concepts in:</p></p><p><p></p></p><p><p><strong>1.Message</strong></p></p><p><p>A message object, as the name implies, is the class that records message Information. There are several more important fields for this class:</p></p><p><p>A.arg1 and Arg2: We can use two fields to hold the integer value we need to pass, and in the service we can store the service ID.</p></p><p><p>B.obj: This field is an object type, and we can let the field pass a number of items to the recipient of the Message.</p></p><p><p>C.what: This field can be said to be a message flag, in the message processing, we can according to the different values of this field to do different processing, similar to when we handle the button event, through switch (v.getid ()) to determine which button is Clicked.</p></p><p><p>When using the message, we can create a message instance from the new message (), but Android recommends that we pass Message.obtain () or Handler.obtainmessage () Gets the message Object. This is not necessarily a straightforward creation of a new instance, but rather a message instance that is immediately available from the pool of messages, and is then fetched and returned to this instance. conversely, If there is no message instance available in the pool, a new message object is given according to the Parameter. By analyzing the source, it is known that the Android system instantiates 10 message objects in a messaging pool by Default.</p></p><p><p></p></p><p><p><strong>2.MessageQueue</strong></p></p><p><p>Message queuing, which holds the data structure of the message object and stores the message according to the "fifo" Principle. Storage is not a meaningful save, but instead the message object is concatenated in a linked List. MessageQueue objects do not need to be created by ourselves, but have Looper objects to manage them, and a thread can have at most one Messagequeue. We can get the MessageQueue in the current thread through Looper.myqueue ().</p></p><p><p></p></p><p><p><strong>3.Looper</strong></p></p><p><p>MessageQueue manager, in a thread, if there is a looper object, there must be a MessageQueue object, and there is only one Looper object and one MessageQueue Object. If the Looper object exists in our thread, we can get it through looper.mylooper (), and we can get the Looper object of the main thread in the current application via Looper.getmainlooper (). One thing to note in this place is that if the Looper object is in the main thread of the application, looper.mylooper () and Looper.getmainlooper () get the same object.</p></p><p><p></p></p><p><p><strong>4.Handler</strong></p></p><p><p>The processor of the Message. Through the handler object we can encapsulate the message object and then add the message object to the MessageQueue through SendMessage (msg), and when MessageQueue loops to the message, The Handlemessage () method of the handler object corresponding to the message object is called to process it. Because the messages are processed in the Handlemessage () method, we should write a class that inherits from handler and then handles the operations we need in Handlemessage ().</p></p><p><p></p></p><p><p>In addition, we know that the Android UI operation is not thread-safe, so it is not possible to update the UI in a child thread. however, Andriod provides several ways to notify the UI thread update interface in a child thread:</p></p><p><p></p></p> <ul> <ul> <li>Activity.runonuithread (Runnable)</li> <li>View.post (Runnable)</li> <li>view.postdelayed (Runnable, Long)</li> <li>Handler</li> </ul> </ul><p><p>It is more common to use handler to receive the data sent by the handler, and to update the UI with this data in conjunction with the main thread. then, as long as the handler object is created in the main thread and the SendMessage method of handler is called in the child thread, the message is placed in the message queue of the main thread. The Handler's Handlemessage method will be called in the handler main thread to process the Message.</p></p><p><p></p></p> <ol class="dp-j"> <li><li><span class="keyword">Package com.superonion;</span></li></li> <li><li></li></li> <li><li><span class="keyword">Import android.app.Activity;</span></li></li> <li><li><span class="keyword">Import android.os.Bundle;</span></li></li> <li><li><span class="keyword">Import android.os.Message;</span></li></li> <li><li><span class="keyword">Import android.util.Log;</span></li></li> <li><li><span class="keyword">Import android.os.Handler;</span></li></li> <li><li></li></li> <li><li><span class="keyword">Public <span class="keyword">class MyHandler <span class="keyword">extends Activity {</span> </span></span></li></li> <li><li><span class="keyword">static <span class="keyword">final String TAG = <span class="string">"Handler"; </span></span></span></li></li> <li><li>Handler h = <span class="keyword">new Handler () {</span></li></li> <li><li><span class="keyword">public <span class="keyword">void Handlemessage (Message msg)</span> </span></li></li> <li><li>{</li></li> <li><li><span class="keyword">Switch (msg.what)</span></li></li> <li><li>{</li></li> <li><li>case <span class="keyword">handler_test:</span></li></li> <li><li>LOG.D (TAG, <span class="string">"the handler thread ID =" + thread.currentthread (). getId () + <span class="string">"\ n"); </span></span></li></li> <li><li>break <span class="keyword">; </span></li></li> <li><li>}</li></li> <li><li>}</li></li> <li><li>};</li></li> <li><li></li></li> <li><li><span class="keyword">static <span class="keyword">final <span class="keyword">int handler_test = <span class="number">1; </span></span></span></span></li></li> <li><li><span class="comment">/** Called when the activity is first Created. *</span> /</li></li> <li><li><span class="annotation">@Override</span></li></li> <li><li><span class="keyword">public <span class="keyword">void OnCreate (Bundle Savedinstancestate) {</span> </span></li></li> <li><li><span class="keyword">super.oncreate (savedinstancestate); </span></li></li> <li><li>LOG.D (TAG, <span class="string">"the main thread ID =" + thread.currentthread (). getId () + <span class="string">"\ n"); </span></span></li></li> <li><li></li></li> <li><li><span class="keyword">new MyThread (). start (); </span></li></li> <li><li>Setcontentview (r.layout.main);</li></li> <li><li>}</li></li> <li><li></li></li> <li><li><span class="keyword">class MyThread <span class="keyword">extends Thread</span> </span></li></li> <li><li>{</li></li> <li><li><span class="keyword">public <span class="keyword">void run ()</span> </span></li></li> <li><li>{</li></li> <li><li>Message msg = <span class="keyword">New Message (); </span></li></li> <li><li>Msg.what = handler_test;</li></li> <li><li>H.sendmessage (msg);</li></li> <li><li>LOG.D (TAG, <span class="string">"the worker thread ID =" + thread.currentthread (). getId () + <span class="string">"\ n"); </span></span></li></li> <li><li>}</li></li> <li><li>}</li></li> <li><li>}</li></li> </ol><strong><strong><br></strong></strong> <ol class="dp-j" start="1"> <li class="alt"><li class="alt"><span class="keyword">Package com.superonion;</span></li></li> <li><li></li></li> <li class="alt"><li class="alt"><span class="keyword">Import android.app.Activity;</span></li></li> <li><li><span class="keyword">Import android.os.Bundle;</span></li></li> <li class="alt"><li class="alt"><span class="keyword">Import android.os.Message;</span></li></li> <li><li><span class="keyword">Import android.util.Log;</span></li></li> <li class="alt"><li class="alt"><span class="keyword">Import android.os.Handler;</span></li></li> <li><li></li></li> <li class="alt"><li class="alt"><span class="keyword">Public <span class="keyword">class MyHandler <span class="keyword">extends Activity {</span> </span></span></li></li> <li><li><span class="keyword">static <span class="keyword">final String TAG = <span class="string">"Handler"; </span></span></span></li></li> <li class="alt"><li class="alt">Handler h = <span class="keyword">new Handler () {</span></li></li> <li><li><span class="keyword">public <span class="keyword">void Handlemessage (Message msg)</span> </span></li></li> <li class="alt"><li class="alt">{</li></li> <li><li><span class="keyword">Switch (msg.what)</span></li></li> <li class="alt"><li class="alt">{</li></li> <li><li>case <span class="keyword">handler_test:</span></li></li> <li class="alt"><li class="alt">LOG.D (TAG, <span class="string">"the handler thread ID =" + thread.currentthread (). getId () + <span class="string">"\ n"); </span></span></li></li> <li><li>break <span class="keyword">; </span></li></li> <li class="alt"><li class="alt">}</li></li> <li><li>}</li></li> <li class="alt"><li class="alt">};</li></li> <li><li></li></li> <li class="alt"><li class="alt"><span class="keyword">static <span class="keyword">final <span class="keyword">int handler_test = <span class="number">1; </span></span></span></span></li></li> <li><li><span class="comment">/** Called when the activity is first Created. *</span> /</li></li> <li class="alt"><li class="alt"><span class="annotation">@Override</span></li></li> <li><li><span class="keyword">public <span class="keyword">void OnCreate (Bundle Savedinstancestate) {</span> </span></li></li> <li class="alt"><li class="alt"><span class="keyword">super.oncreate (savedinstancestate); </span></li></li> <li><li>LOG.D (TAG, <span class="string">"the main thread ID =" + thread.currentthread (). getId () + <span class="string">"\ n"); </span></span></li></li> <li class="alt"><li class="alt"></li></li> <li><li><span class="keyword">new MyThread (). start (); </span></li></li> <li class="alt"><li class="alt">Setcontentview (r.layout.main);</li></li> <li><li>}</li></li> <li class="alt"><li class="alt"></li></li> <li><li><span class="keyword">class MyThread <span class="keyword">extends Thread</span> </span></li></li> <li class="alt"><li class="alt">{</li></li> <li><li><span class="keyword">public <span class="keyword">void run ()</span> </span></li></li> <li class="alt"><li class="alt">{</li></li> <li><li>Message msg = <span class="keyword">New Message (); </span></li></li> <li class="alt"><li class="alt">Msg.what = handler_test;</li></li> <li><li>H.sendmessage (msg);</li></li> <li class="alt"><li class="alt">LOG.D (TAG, <span class="string">"the worker thread ID =" + thread.currentthread (). getId () + <span class="string">"\ n"); </span></span></li></li> <li><li>}</li></li> <li class="alt"><li class="alt">}</li></li> <li><li>}</li></li> </ol><p><p></p></p><p><p></p></p><p><p>In the code above, when handler is created in the main thread, the child thread can send the message to the main thread through the SendMessage () method and process it in the Handlemessage () method.</p></p><p><p></p></p><p><p>About the UI update mechanism and misconceptions of Android</p></p></span>
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.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service