android消息机制
2019獨角獸企業重金招聘Python工程師標準>>>
主要有四大類Message,?Looper, MessageQueue, Handler
1.?
一個線程有一個Looper,每個Looper都有一個MessageQueue.
2.
MessageQueue是Message的一個容器,是個鏈表
3.?
Message的結構有幾個比較有意思的變量
Runnable callback; ?
Handler target;
4.
Looper的關鍵函數就是一個循環,不停去獲取MessageQueue中的Message,執行msg.target.dispatchMessage
? ? /**
???? * Run the message queue in this thread. Be sure to call
???? * {@link #quit()} to end the loop.
???? */
??? public static void loop() {
? ? ?...
??????? for (;;) {
??????????? Message msg = queue.next(); // might block
??????????? if (msg == null) {
??????????????? // No message indicates that the message queue is quitting.
??????????????? return;
??????????? }
? ? ? ? ? ...
??????????? msg.target.dispatchMessage(msg);
? ? ? ? ? ...
??????????? msg.recycle();
??????? }
??? }
? ? /**
???? * Handle system messages here.
???? */
??? public void dispatchMessage(Message msg) {
??????? if (msg.callback != null) {
??????????? handleCallback(msg);
??????? } else {
??????????? if (mCallback != null) {
??????????????? if (mCallback.handleMessage(msg)) {
??????????????????? return;
??????????????? }
??????????? }
??????????? handleMessage(msg);
??????? }
??? }
轉載于:https://my.oschina.net/u/1445604/blog/492151
總結
以上是生活随笔為你收集整理的android消息机制的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iOS响应事件
- 下一篇: thinkphp url缩短