码迷,mamicode.com
首页 > 移动开发 > 详细

Android 子线程 更新 UI 界面 总结

时间:2015-05-21 17:25:03      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:android   技术   java   线程池   多线程   

package com.jrhcode.morethreadtest;

import java.util.concurrent.Executors;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 
 * 主要功能是:在子线程中来更改TextView的显示内容
 * 
 * android 多线程编程
   1:通过handler 在新启动的线程中更新UI组件 Handler.post(new Runnable());
   2:handler.postdelayed(new Runnable, delay);
   3: Activity.runOnUiThread(); 
   4:AsyncTask 
   5:线程池
 * 
 * @author Administrator
 * 
 */
public class MainActivity extends Activity {

	TextView tv;
	private Handler mHandler;
	private String TAG = MainActivity.class.getSimpleName();
	int i = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		tv = (TextView) findViewById(R.id.tv);
		mHandler = new Handler();
		// initHandler();
		// initHandler2();
		// initHandler3();
		// updateTv();
		// updateTVV();
		threedPool();
	}

	private void threedPool() {
		// TODO Auto-generated method stub
		Executors.newSingleThreadExecutor().submit(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				System.out.println(Thread.currentThread().getName());
				tv.setText(TAG);
				mHandler.post(new Runnable() {

					@Override
					public void run() {
						// TODO Auto-generated method stub
						tv.setText("this is handler post");
					}
				});
			}
		});

	}

	/**
 * 
 */
	private void updateTVV() {
		// TODO Auto-generated method stub
		new MyTask().execute();
	}

	/**
	 * 方法3
	 */
	private void updateTv() {
		// TODO Auto-generated method stub
		new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				MainActivity.this.runOnUiThread(new Runnable() {

					@Override
					public void run() {
						// TODO Auto-generated method stub
						tv.setText(TAG);
					}
				});
			}
		}).start();
	}

	/**
	 * 延迟3秒
	 */
	private void initHandler3() {
		// TODO Auto-generated method stub
		new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub

				mHandler.postDelayed(new Runnable() {

					@Override
					public void run() {
						// TODO Auto-generated method stub
						tv.setText(TAG);
					}
				}, 3000);
			}
		}).start();
	}

	/**
	 * 在子线程中方法
	 */
	private void initHandler2() {
		// TODO Auto-generated method stub
		new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				mHandler.post(new Runnable() {

					@Override
					public void run() {
						System.out.println(Thread.currentThread().getName());
						tv.setText(TAG);
					}
				});
			}
		}).start();

	}

	/**
	 * Mothod 1; 该方法是在注线程中操作UI;
	 */
	private void initHandler() {
		// TODO Auto-generated method stub

		mHandler.post(new Runnable() {

			@Override
			public void run() {
				Toast.makeText(MainActivity.this,
						Thread.currentThread().getName(), 0).show();
				tv.setText(TAG);
			}
		});
	}

	class MyTask extends AsyncTask<String, Void, String> {

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);

			tv.setText(TAG);
		}

	}

}

Android 子线程 更新 UI 界面 总结

标签:android   技术   java   线程池   多线程   

原文地址:http://blog.csdn.net/jiaruihua_blog/article/details/45894459

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!