Android消息處理的大體的原理以下:html
1.有一個消息隊列,能夠往隊列中添加消息java
2.有一個消息循環,能夠從消息隊列中取出消息async
Android系統中這些工做主要由Looper和Handler兩個類來實現:ide
Looper類: 有一個消息隊列,封裝消息循環函數
Handler類: 消息的投遞、消息的處理oop
Looper類:ui
Looper的使用需先調用 Looper.prepare(),而後調用Looper.loop()開啓消息循環。this
1 public static void prepare() { 2 prepare(true); 3 } 4 5 private static void prepare(boolean quitAllowed) { 6 if (sThreadLocal.get() != null) { 7 throw new RuntimeException("Only one Looper may be created per thread"); 8 } 9 sThreadLocal.set(new Looper(quitAllowed)); 10 }
static final ThreadLocal<Looper> sThreadLocal = new ThreadLocal<Looper>();
prepare會在調用線程的局部變量中設置一個Looper對象;spa
ThreadLocal是java中線程局部變量類,有兩個關鍵函數:線程
set: 設置調用線程的局部變量
get: 獲取調用線程的局部變量
Looper的構造函數:
1 private Looper(boolean quitAllowed) { 2 mQueue = new MessageQueue(quitAllowed); 3 mThread = Thread.currentThread(); 4 }
建立了一個消息隊列,用於存放消息。
Looper.loop(), myLooper()經過ThreadLocal對象獲取了prepare時建立的Looper對象。loop裏面是一個循環,循環從MessageQueue中取消息,而後經過Handler去處理。
(msg.target.dispatchMessage(msg); target是一個Handler對象,後面會提到)
1 public static @Nullable Looper myLooper() { 2 return sThreadLocal.get(); 3 } 4 5 public static void loop() { 6 final Looper me = myLooper(); 7 if (me == null) { 8 throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread."); 9 } 10 final MessageQueue queue = me.mQueue; 11 12 // Make sure the identity of this thread is that of the local process, 13 // and keep track of what that identity token actually is. 14 Binder.clearCallingIdentity(); 15 final long ident = Binder.clearCallingIdentity(); 16 17 for (;;) { 18 Message msg = queue.next(); // might block 可能會阻塞 19 if (msg == null) { 20 // No message indicates that the message queue is quitting. 21 return; 22 } 23 24 // This must be in a local variable, in case a UI event sets the logger 25 Printer logging = me.mLogging; 26 if (logging != null) { 27 logging.println(">>>>> Dispatching to " + msg.target + " " + 28 msg.callback + ": " + msg.what); 29 } 30 31 msg.target.dispatchMessage(msg); 32 33 if (logging != null) { 34 logging.println("<<<<< Finished to " + msg.target + " " + msg.callback); 35 } 36 37 // Make sure that during the course of dispatching the 38 // identity of the thread wasn't corrupted. 39 final long newIdent = Binder.clearCallingIdentity(); 40 if (ident != newIdent) { 41 Log.wtf(TAG, "Thread identity changed from 0x" 42 + Long.toHexString(ident) + " to 0x" 43 + Long.toHexString(newIdent) + " while dispatching to " 44 + msg.target.getClass().getName() + " " 45 + msg.callback + " what=" + msg.what); 46 } 47 48 msg.recycleUnchecked(); 49 } 50 }
Looper的做用:
封裝一個消息隊列
prepare()方法把Looper對象和調用的線程綁定起來
經過loop()方法處理消息隊列中的消息
Hander類:
Handler有多個構造函數,經常使用的就下面幾個:
1 public Handler() { 2 this(null, false); 3 } 4 5 public Handler(Looper looper) { 6 this(looper, null, false); 7 } 8 9 public Handler(Callback callback, boolean async) { 10 mLooper = Looper.myLooper(); 11 if (mLooper == null) { 12 throw new RuntimeException( 13 "Can't create handler inside thread that has not called Looper.prepare()"); 14 } 15 mQueue = mLooper.mQueue; 16 mCallback = callback; 17 mAsynchronous = async; 18 } 19 20 public Handler(Looper looper, Callback callback, boolean async) { 21 mLooper = looper; 22 mQueue = looper.mQueue; 23 mCallback = callback; 24 mAsynchronous = async; 25 }
無參構造函數,經過Looper.myLooper()獲取調用線程的Looper對象; Handler提供了一個Callback的接口,參數裏面的Callback在處理消息的時候會用到,若是設置了全局Callback,消息會經過這個Callback處理,若是未設置,則需重重載handlerMessage()方法來處理消息。
1 public interface Callback { 2 public boolean handleMessage(Message msg); 3 }
(1) Handler和Message ----> Handler把Message插入Looper的消息隊列。
Handler有一系列的處理消息的函數,好比:
1 public final boolean sendMessage(Message msg) 2 { 3 return sendMessageDelayed(msg, 0); 4 } 5 6 public final boolean sendEmptyMessage(int what) 7 { 8 return sendEmptyMessageDelayed(what, 0); 9 } 10 11 public final boolean sendEmptyMessageDelayed(int what, long delayMillis) { 12 Message msg = Message.obtain(); 13 msg.what = what; 14 return sendMessageDelayed(msg, delayMillis); 15 } 16 17 public final boolean sendMessageDelayed(Message msg, long delayMillis) 18 { 19 if (delayMillis < 0) { 20 delayMillis = 0; 21 } 22 return sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis); 23 } 24 25 public boolean sendMessageAtTime(Message msg, long uptimeMillis) { 26 MessageQueue queue = mQueue; 27 if (queue == null) { 28 RuntimeException e = new RuntimeException( 29 this + " sendMessageAtTime() called with no mQueue"); 30 Log.w("Looper", e.getMessage(), e); 31 return false; 32 } 33 return enqueueMessage(queue, msg, uptimeMillis); 34 } 35 36 public final boolean sendMessageAtFrontOfQueue(Message msg) { 37 MessageQueue queue = mQueue; 38 if (queue == null) { 39 RuntimeException e = new RuntimeException( 40 this + " sendMessageAtTime() called with no mQueue"); 41 Log.w("Looper", e.getMessage(), e); 42 return false; 43 } 44 return enqueueMessage(queue, msg, 0); 45 }
這些都是將消息插入到Looper的消息隊列,sendMessageAtFrontOfQueue()是將消息插入到消息隊列的隊列頭,因此優先級很高。全部方法最後都是經過enqueueMessage()方法插入消息。
1 private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) { 2 msg.target = this; 3 if (mAsynchronous) { 4 msg.setAsynchronous(true); 5 } 6 return queue.enqueueMessage(msg, uptimeMillis); 7 }
msg.target = this,前面有提到,target是Handler對象,消息的處理最後都需經過這個。
(2)Handler的消息處理
上面的Looper.loop()方法中,不斷從消息隊列中提取消息,而後經過Handler的dispatchMessage()方法處理消息。
1 public void dispatchMessage(Message msg) { 2 if (msg.callback != null) { 3 handleCallback(msg); 4 } else { 5 if (mCallback != null) { 6 if (mCallback.handleMessage(msg)) { 7 return; 8 } 9 } 10 handleMessage(msg); 11 } 12 }
若是Message設置了callback,則經過這個callback處理,若是Message沒設置callback則先經過全局callback來處理,若是都沒設置,則經過handlerMessage()方法來處理。
簡單總結一下:
Looper中有一個MessageQueue,裏面存儲一個個待處理的Message。
Message中有一個Handler,這個Handler處理Message。
轉載還望註明出處:http://www.cnblogs.com/xiaojianli/p/5642380.html