android 線程間的通訊

  近來找了一些關於android線程間通訊的資料,整理學習了一下,並製做了一個簡單的例子。java

 andriod提供了 Handler 和 Looper 來知足線程間的通訊。例如一個子線程從網絡上下載了一副圖片,當它下載完成後會發送消息給主線程,這個消息是經過綁定在主線程的Handler來傳遞的。android

在Android,這裏的線程分爲有消息循環的線程和沒有消息循環的線程,有消息循環的線程通常都會有一個Looper,這個事android的新 概念。咱們的主線程(UI線程)就是一個消息循環的線程。針對這種消息循環的機制,咱們引入一個新的機制Handle,咱們有消息循環,就要往消息循環裏 面發送相應的消息,自定義消息通常都會有本身對應的處理,消息的發送和清除,消息的的處理,把這些都封裝在Handle裏面,注意Handle只是針對那 些有Looper的線程,無論是UI線程仍是子線程,只要你有Looper,我就能夠往你的消息隊列裏面添加東西,並作相應的處理。
可是這裏還有一點,就是隻要是關於UI相關的東西,就不能放在子線程中,由於子線程是不能操做UI的,只能進行數據、系統等其餘非UI的操做。

  在Android,這裏的線程分爲有消息循環的線程和沒有消息循環的線程,有消息循環的線程通常都會有一個Looper,這個是android的新概念。咱們的主線程(UI線程)就是一個消息循環的線程。針對這種消息循環的機制,咱們引入一個新的機制Handler,咱們有消息循環,就要往消息循環裏面發送相應的消息,自定義消息通常都會有本身對應的處理,消息的發送和清除,把這些都封裝在Handler裏面,注意Handler只是針對那 些有Looper的線程,無論是UI線程仍是子線程,只要你有Looper,我就能夠往你的消息隊列裏面添加東西,並作相應的處理。網絡

可是這裏還有一點,就是隻要是關於UI相關的東西,就不能放在子線程中,由於子線程是不能操做UI的,只能進行數據、系統等其餘非UI的操做。ide

 

  一個Handler的建立它就會被綁定到這個線程的消息隊列中,若是是在主線程建立的,那就不須要寫代碼來建立消息隊列了,默認的消息隊列會在主線程被建立。可是若是是在子線程的話,就必須在建立Handler以前先初始化線程的消息隊列。以下面的代碼:函數

 

class ChildThread extends Thread {

    public void run() {

        /*
         * 建立 handler前先初始化Looper.
         */
        Looper.prepare();

        /*
         * 在子線程建立handler,因此會綁定到子線程的消息隊列中
         *
         */
        mChildHandler = new Handler() {

            public void handleMessage(Message msg) {

                /*
                 * Do some expensive operations there.
                 */
            }
        };

        /*
         * 啓動該線程的消息隊列
         */
        Looper.loop();
    }
}

 

 

當Handler收到消息後,就會運行handleMessage(…)的回調函數,能夠在裏面作一些耗時的操做。oop


最後完成了操做要結束子線程時,記得調用quit()來結束消息循環隊列。學習

 

 

 

mChildHandler.getLooper().quit();

下面是一個線程間通訊的小例子:ui

 

 

 

 

 

 

 

/**
 * 
 * @author allin.dev 
 * http://allin.cnblogs.com
 * 
 */
public class MainThread extends Activity {

	private static final String TAG = "MainThread";
	private Handler mMainHandler, mChildHandler;
	private TextView info;
	private Button msgBtn;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		info = (TextView) findViewById(R.id.info);
		msgBtn = (Button) findViewById(R.id.msgBtn);

		mMainHandler = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				Log.i(TAG, "Got an incoming message from the child thread - "
						+ (String) msg.obj);
				// 接收子線程的消息
				info.setText((String) msg.obj);
			}

		};

		new ChildThread().start();
		
		
		msgBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				
				if (mChildHandler != null) {
					
					//發送消息給子線程
					Message childMsg = mChildHandler.obtainMessage();
					childMsg.obj = mMainHandler.getLooper().getThread().getName() + " says Hello";
					mChildHandler.sendMessage(childMsg);
					
					Log.i(TAG, "Send a message to the child thread - " + (String)childMsg.obj);


				}
			}
		});

	}

	public void onDestroy() {
      super.onDestroy();
		Log.i(TAG, "Stop looping the child thread's message queue");

		mChildHandler.getLooper().quit();
	}

	class ChildThread extends Thread {

		private static final String CHILD_TAG = "ChildThread";

		public void run() {
			this.setName("ChildThread");

			//初始化消息循環隊列,須要在Handler建立以前
			Looper.prepare();

			mChildHandler = new Handler() {
				@Override
				public void handleMessage(Message msg) {
					 Log.i(CHILD_TAG, "Got an incoming message from the main thread - " + (String)msg.obj);


					try {

						//在子線程中能夠作一些耗時的工做
						sleep(100);

						Message toMain = mMainHandler.obtainMessage();
						toMain.obj = "This is " + this.getLooper().getThread().getName() +
									".  Did you send me \"" + (String)msg.obj + "\"?";

						mMainHandler.sendMessage(toMain);

						Log.i(CHILD_TAG, "Send a message to the main thread - " + (String)toMain.obj);

					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}

			};

			Log.i(CHILD_TAG, "Child handler is bound to - "+ mChildHandler.getLooper().getThread().getName());

			//啓動子線程消息循環隊列
			Looper.loop();
		}
	}
}

 

[源碼下載]this

相關文章
相關標籤/搜索