1. Handler
public class SecondActivity extends Activity { private static final int MSG_WHAT = 101; TextView tv; Button btn; private MyHadler mHandler1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView(R.layout.activity_main2); tv = (TextView) findViewById(R.id.tv); btn = (Button) findViewById(R.id.btn); mHandler1 = new MyHadler(); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { updateUi(); } }); } class MyHadler extends Handler { @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case MSG_WHAT: String str = (String) msg.obj; tv.setText(str); } } }; /** * */ private void updateUi1() { new Thread() { @Override public void run() { try { Thread.sleep(5 * 1000); } catch (InterruptedException e) { e.printStackTrace (); } Message message = mHandler1.obtainMessage(); message.what = MSG_WHAT; message.obj = "Data from child thread"; mHandler1.sendMessage(message); } }.start(); } }
2. Handler's post() method
private void updateUi2() { new Thread() { @Override public void run() { try { Thread.sleep(5 * 1000); } catch (InterruptedException e) { e.printStackTrace (); } mHandler1.post(new Runnable() { @Override public void run() { tv.setText("Handler.post update UI"); } }); } }.start(); }
Look at the source code of the post() method
public final boolean post(Runnable r) { return sendMessageDelayed(getPostMessage(r), 0); }
This method calls sendMessageDelayed(getPostMessage(r), 0) to send a message. Let's first look at the getPostMessage() method
private static Message getPostMessage(Runnable r) { Message m = Message.obtain(); m.callback = r; return m; }
The getPostMessage() method converts the Runnable object into the callback property of the message object Message. The sendMessageDelayed() method will not say much, and it will return to the processing mechanism of Handler.
Handler's dispatchMessage() method
public void dispatchMessage(Message msg) { if (msg.callback != null) { handleCallback(msg); } else { if (mCallback != null) { if (mCallback.handleMessage(msg)) { return; } } handleMessage(msg); } }
As you can see, first judge that if the callback of the message is not equal to null, call the handleCallback() method of the Handler, otherwise call the handleMessage() method of the Handler object.
3. Activity's runOnUiThread() method
/** * Update UI through runOnUiThread */ private void updateUi3(){ new Thread(){ @Override public void run() { try { Thread.sleep(3*1000); } catch (InterruptedException e) { e.printStackTrace (); } runOnUiThread(new Runnable() { @Override public void run() { tv.setText("runOnUiThread更新UI"); } }); } }.start(); }
Take a look at the source code of the runOnUiThread() method
public final void runOnUiThread(Runnable action) { if (Thread.currentThread() != mUiThread) { mHandler.post(action); } else { action.run(); } }
4.View.post(Runnable r) method
private void updateUi4(){ new Thread(){ @Override public void run() { super.run(); tv.post(new Runnable() { @Override public void run() { tv.setText("view post更新UI"); } }); } }.start(); }
Let's take a look at the source code of the post() method
public boolean post(Runnable action) { final AttachInfo attachInfo = mAttachInfo; if (attachInfo != null) { return attachInfo.mHandler.post(action); } // Postpone the runnable until we know on which thread it needs to run. // Assume that the runnable will be successfully placed after attach. getRunQueue().post(action); return true; }
This method still calls the post() method of the Handler. Handler's post() method is very important!