且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Handler详解系列(六)——View的post()方法详解

更新时间:2021-11-17 16:22:31

MainActivity如下:
package cc.testui2;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;
/**
 * Demo描述:
 * 在子线程中更改UI的方式二
 * 
 * 在子线程中采用View的post()方法.
 * 依据源码可知它在最终还是调用了UI线程的handler的post()方法.
 * 所以在post方法中勿做耗时操作
 * 
 * 看一下该方法的源码:
 *
 * Causes the Runnable to be added to the message queue.
 * The runnable will be run on the user interface thread.
 *
 * @param action The Runnable that will be executed.
 *
 * @return Returns true if the Runnable was successfully placed in to the
 *         message queue.  Returns false on failure, usually because the
 *         looper processing the message queue is exiting.
 *
 * public boolean post(Runnable action) {
 *      final AttachInfo attachInfo = mAttachInfo;
 *      if (attachInfo != null) {
 *         return attachInfo.mHandler.post(action);
 *      }
 *      // Assume that post will succeed later
 *      ViewRootImpl.getRunQueue().post(action);
 *      return true;
 * }
 * 
 * 
 * 该方法的英文注释:
 * 将Runnable放入消息队列,并在UI线程中执行.
 * 
 * 参考资料:
 * http://blog.csdn.net/guolin_blog/article/details/9991569
 * Thank you very much
 */

public class MainActivity extends Activity {
	 private TextView mTextView;
	 private TextView mTipTextView;
	 private Button mButton;
		@Override
		protected void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.main);
			init();
		}
	    private void init(){
	    	mTextView=(TextView) findViewById(R.id.textView);
	    	mTipTextView=(TextView) findViewById(R.id.tipTextView);
	    	mButton=(Button) findViewById(R.id.button);
	    	mButton.setOnClickListener(new OnClickListenerImpl());
	    	System.out.println("UI线程ID="+Thread.currentThread().getId());
	    }
		
	private class OnClickListenerImpl implements OnClickListener {
		@Override
		public void onClick(View v) {
			mTipTextView.post(new Runnable() {
				@Override
				public void run() {
					mTextView.setText("My number is 9527");
					System.out.println("view.post(new Runnable())里的run()方法 线程ID="+Thread.currentThread().getId());
				}
			});
		}
	}
}

main.xml如下:
<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:id="@+id/tipTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="测试在子线程中更新UI" 
        android:layout_centerInParent="true"
        />

</RelativeLayout>