Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 子線程 更新 UI 界面 總結

Android 子線程 更新 UI 界面 總結

編輯:關於Android編程

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 {

		@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);
		}

	}

}

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved