Handler detail series (5) -- Handler's post () method details, handlerpost
MainActivity is as follows:
Package cc. testui1; import android. OS. bundle; import android. OS. handler; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. textView; import android. app. activity;/*** Demo Description: * change the UI mode in the Child thread. ** use the post () of the Handler of the main thread in the Child thread () method * change the UI. The principle and form of sendMessage () in the Child thread are similar. ** detailed analysis: * When Handler calls its post () method, the call sequence of the method is as follows: * Handler's post () method ---> Handler's send MessageDelayed () method ---> * Handler's sendMessageAtTime () method. * Now we have returned to the familiar sendMessageAtTime () ** detailed code: * public final boolean post (Runnable r) {* return sendMessageDelayed (getPostMessage (r), 0 ); *} *** private final Message getPostMessage (Runnable r) {* Message m = Message. obtain (); * m. callback = r; * return m; *} *** public final boolean sendMessageDelayed (Message msg, long delayMillis ){* If (delayMillis <0) {* delayMillis = 0; *} * return sendMessageAtTime (msg, SystemClock. uptimeMillis () + delayMillis); **} *** in this detailed process, we can see that in post (Runnable r), the input parameter r of this method calls getPostMessage (r) * encapsulate r into a Message. * The focus is on m in the getPostMessage (r) method. callback = r * assigned the value of r to the callback! Of the Message !!!!! ** Let's review the dispatchMessage () method called when a player leaves the queue. * Next, let's look at the dispatchMessage (Message msg) method of Handler: * public void dispatchMessage (Message msg) {* // 1 message callback * if (msg. callback! = Null) {* handleCallback (msg); *} else {* // 2 handler's callback * if (mCallback! = Null) {* if (mCallback. handleMessage (msg) {* return; *} * // handleMessage () * handleMessage (msg) of 3 Handler ); *} **} *** at 1, it is determined that the callback of the Message is not null, so handleCallback (msg) is called. * The detailed code is as follows: * private static void handleCallback (Message message) {* message. callback. run (); *} * This method is relatively simple: directly call the run () method of the post (Runnable r) input parameter (Runnable object. ** conclusion: * 1. Use post (Runnable r) to update the UI, principles, and sendMessage () classes in the Child thread. Like. * 2 Note: * in the following example, mHandler is the Handler of the main thread. * therefore, the statement "using post (Runnable r) to update the UI in a child thread" is not particularly accurate. * specifically, the UI is updated by sending a message to the Message Queue of the main thread in the Child thread. * 3 calling post (Runnable r) will not start a new thread. UI updates are completed in the main thread !!!!!!!!!! * In the following example, two output thread IDs are the same. * So do not perform time-consuming operations in the post method *** reference: * http://blog.csdn.net/guolin_blog/article/details/9991569 * Thank you very much */public class MainActivity extends Activity {private TextView mTextView; private Handler mHandler; private Button mButton; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); init ();} private void init () {mHandler = new Handler (); mTextView = (TextView) findViewById (R. id. textView); mButton = (Button) findViewById (R. id. button); mButton. setOnClickListener (new OnClickListenerImpl (); System. out. println ("UI Thread ID =" + Thread. currentThread (). getId ();} private class OnClickListenerImpl implements OnClickListener {@ Overridepublic void onClick (View v) {new Thread () {public void run () {mHandler. post (new Runnable () {@ Overridepublic void run () {System. out. in println ("post (Runnable r), the Thread ID =" + Thread. currentThread (). getId (); mTextView. setText ("My number is 007 ");}});};}. start ();}}}
Main. xml is as follows:
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent"> <TextView android: id = "@ + id/textView" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "test" android: layout_centerHorizontal = "true" android: layout_marginTop = "50dip"/> <Button android: id = "@ + id/button" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Button" android: layout_centerHorizontal = "true" android: layout_marginTop = "120dip"/> <TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Update UI in child thread" android: layout_centerInParent = "true"/> </RelativeLayout>
Post method for Android Handler
This Handler class exists in Android, but you have defined a Handler class here, so you use your own Handler class, so the above error will be reported.
What is the role of the post method of Handler in android?
Generally, a time-consuming task is executed in a work thread. When the task is completed, the UI thread is returned, which is generally used to update the UI. There are two ways to achieve this purpose.
One is handler. sendMessage. Send a message and then execute the relevant task code based on the message.
The other is handler. post (r ). R is the task code to be executed. This means that the r code is actually executed in the UI thread. You can write code to update the UI. (The Working thread cannot update the UI)