Android消息隊列(一)--Handler使用

一,使用Handler的流程

1,創建Handler對象

創建Handler的兩種方法

使用無參構造函數創建;繼承Handler類,並實現handlerMessage方法

2,發送消息

在事件監聽器中調用Handler的post方法,將要執行的線程對象添加到線程隊列中,將要執行的操作寫在線程對象的run方法中,一般是一個Runnable對象,複寫其中的run方法

Handler對象管理了兩個隊列,一個是線程隊列(post),一個是消息隊列(sendMessage)

注:如果想要這個流程一直執行的話,可以在run方法內部執行postDelayed或者post方法,再將該線程對象添加到消息隊列中,重複執行。想要線程停止執行,調用Handler對象的removeCallbacks(Runnable r) 方法從線程隊列中移除線程對象,使線程停止執行。Handler爲Android提供了一種異步消息處理機制,當向消息隊列中發送消息(sendMessage)後就立即返回,而從消息隊列中讀取消息時會阻塞,其中從消息隊列中讀取消息時會執行Handler中的public void handleMessage(Message msg) 方法,因此在創建Handler時應該使用匿名內部類重寫該方法,在該方法中寫上讀取到消息後的操作,使用Handler的obtainMessage() 來獲得消息對象。

3,消息處理

Message

即Message,可以傳遞一些信息,可以使用arg1.arg2,Object傳遞一些整形或者對象,還可以使用Message對象的setData(Bundle bundle)來講Bundle對象傳遞給新創建的線程,新創建的線程在執行handleMessage(Message msg)時可以從message中利用getData()提取出Bundle對象來進行處理。

Handler與線程的關係

Each Handler instance is associated with a single thread and that thread's message queue. When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it -- from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue

在main thread中執行消息處理過程

使用Handler的post方法將Runnable對象放到Handler的線程隊列中後,該Runnable的執行其實並未單獨開啓線程,而是仍然在當前Activity線程中執行的,Handler只是調用了Runnable對象的run方法

創建新的線程來執行消息處理過程

首先生成一個HandlerThread對象,實現了使用Looper來處理消息隊列的功能,這個類由Android應用程序框架提供
HandlerThread handlerThread = new HandlerThread("handler_thread");
在使用HandlerThread的getLooper()方法之前,必須先調用該類的start();
handlerThread.start();
根據這個HandlerThread對象得到其中的Looper對象。
創建自定義的繼承於Handler類的子類,其中實現一個參數爲Looper對象的構造方法,方法內容調用父類的構造函數即可。
使用第三步得到的Looper對象創建自定義的Handler子類的對象,再將消息(Message)發送到該Handler的消息隊列中,Handler複寫的handleMessage()將會執行來處理消息隊列中的消息。

4,代碼示例

public class HandlerTest2 extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		//打印了當前線程的ID
		System.out.println("Activity-->" + Thread.currentThread().getId());
		//生成一個HandlerThread對象,實現了使用Looper來處理消息隊列的功能,這個類由Android應用程序框架提供
		HandlerThread handlerThread = new HandlerThread("handler_thread");
		//在使用HandlerThread的getLooper()方法之前,必須先調用該類的start();
		handlerThread.start();
		MyHandler myHandler = new MyHandler(handlerThread.getLooper());
		Message msg = myHandler.obtainMessage();
		//將msg發送到目標對象,所謂的目標對象,就是生成該msg對象的handler對象
		Bundle b = new Bundle();
		b.putInt("age", 20);
		b.putString("name", "Jhon");
		msg.setData(b);
		msg.sendToTarget();
	}
	
	class MyHandler extends Handler{
		public MyHandler(){
			
		}
		public MyHandler(Looper looper){
			super(looper);
		}
		@Override
		public void handleMessage(Message msg) {
			Bundle b = msg.getData();
			int age = b.getInt("age");
			String name = b.getString("name");
			System.out.println("age is " + age + ", name is" + name);
			System.out.println("Handler--->" + Thread.currentThread().getId());
			System.out.println("handlerMessage");
		}
	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章