原创

Android子线程中更新UI的三种方法和获取message的两种方法总结

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://gaolei.blog.csdn.net/article/details/49585835

       转载请标明原地址:http://write.blog.csdn.net/postedit/49585835      

       遇到知识善于归纳总结,这样能够帮助自己全面了解知识和熟练掌握知识。下面我就把Android主线程和子线程交互时,子线程中更新UI的三种方法和获取message的两种方法总结一下;主线程和子线程交互时,如果在主线程更新UI相信大家都知道1.Handler  2.AsyncTask。下面我就把代码贴出来,里面有注释。



<span style="font-size:14px;">public class MainActivity extends Activity implements OnClickListener {

	private Button button1, button2, button3, button4, button5;
	private TextView textView1, textView2, textView3, textView4, textView5;

	private Handler mHandler;
	private SubThread5 handlerThread;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		button1 = (Button) findViewById(R.id.button1);
		button2 = (Button) findViewById(R.id.button2);
		button3 = (Button) findViewById(R.id.button3);
		button4 = (Button) findViewById(R.id.button4);
		button5 = (Button) findViewById(R.id.button5);
		textView1 = (TextView) findViewById(R.id.textView1);
		textView2 = (TextView) findViewById(R.id.textView2);
		textView3 = (TextView) findViewById(R.id.textView3);
		textView4 = (TextView) findViewById(R.id.textView4);
		textView5 = (TextView) findViewById(R.id.textView5);
		button1.setOnClickListener(this);
		button2.setOnClickListener(this);
		button3.setOnClickListener(this);
		button4.setOnClickListener(this);
		button5.setOnClickListener(this);
		new SubThread4().start();
		getWindow().setBackgroundDrawable(null);

	}

	class SubThread1 extends Thread {

		public void run() {
			MainActivity.this.runOnUiThread(new Runnable() {

				@Override
				public void run() {
					// 在这里执行你要想的操作 比如直接在这里更新ui或者调用回调在 在回调中更新ui
					textView1.setText("SubThread1");
				}
			});
		}
	}

	class SubThread2 extends Thread {

		public void run() {
			new Handler(MainActivity.this.getMainLooper()).post(new Runnable() {

				@Override
				public void run() {
					// 在这里执行你要想的操作 比如直接在这里更新ui或者调用回调在 在回调中更新ui
					textView2.setText("SubThread2");
				}
			});
		}
	}

	class SubThread3 extends Thread {

		public void run() {
			textView3.post(new Runnable() {

				@Override
				public void run() {
					// 在这里执行你要想的操作 比如直接在这里更新ui或者调用回调在 在回调中更新ui
					textView3.setText("SubThread3");
				}
			});
		}
	}

	class SubThread4 extends Thread {

		public void run() { 
			Looper.prepare();// 1、初始化Looper
			mHandler = new Handler() {// 2、绑定handler到CustomThread实例的Looper对象
				public void handleMessage(Message msg) {// 3、定义处理消息的方法
					switch (msg.what) {
					case 0:
						// 这里只是能通信 但是不能在子线程改变UI,如果改变textView4的值将会报错(Only the original thread that created a view hierarchy can touch its views)
						//textView4.setText("SubThread4");
						System.out.println("SubThread4");
					}
				}
			};
			Looper.loop();// 4、启动消息循环}
		}
	}

	private class SubThread5 extends HandlerThread implements Callback {

		public SubThread5(String name) {
			super(name);
		}

		@Override
		public boolean handleMessage(Message msg) {
			// 这里只是能通信 但是不能在子线程改变UI,如果改变textView5的值将会报错(Only the original thread that created a view hierarchy can touch its views)
			// textView5.setText("SubThread5");
			System.out.println(" handleMessage CurrentThread = "
					+ Thread.currentThread().getName());
			return true;
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.button1:
			new SubThread1().start();
			break;
		case R.id.button2:
			new SubThread2().start();
			break;
		case R.id.button3:
			new SubThread3().start();
			break;
		case R.id.button4:
			mHandler.sendEmptyMessage(0);
			break;
		case R.id.button5:
			handlerThread = new SubThread5("myHanler");
			handlerThread.start();
			// 注意:
			// 这里必须用到handler的这个构造器,因为需要把callback传进去,从而使自己的HandlerThread的handlerMessage来替换掉Handler原生的handlerThread
			mHandler = new Handler(handlerThread.getLooper(), handlerThread);
			mHandler.sendEmptyMessage(0);
			break;
		}
	}
}</span>


注:1.Handler 与谁相关联不是看声明在什么地方,是看与哪个线程的looper挂钩。默认是主线程的looper.因为主线程中默认就有了looper,消息循环队列

2. public final void runOnUiThread (Runnable action)

Added in API level 1

Runs the specified action on the UI thread. If the current thread is the UI thread, then the action is executed immediately. If the current thread is not the UI thread, the action is posted to the event queue of the UI thread.

如果当前线程是UI主线程,则直接执行Runnable的代码;否则将Runnable post到UI线程的消息队列。

Parameters
action the action to run on the UI thread

Activity.java中runOnUiThread源码

[java] view plaincopy
  1. public final void runOnUiThread(Runnable action) {  
  2.        if (Thread.currentThread() != mUiThread) {  
  3.            mHandler.post(action);//将Runnable Post到消息队列,由内部的mHandler来处理,实际上也是Handler的处理方式  
  4.        } else {  
  5.            action.run();//已经在UI线程,直接运行。  
  6.        }  
  7.    } 



                   源码地址欢迎光临......

文章最后发布于: 2015-11-02 14:20:47
展开阅读全文
0 个人打赏
私信求帮助

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 书香水墨 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览