日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
Android應(yīng)用程序消息處理機(jī)制(Looper、Handler)分析(7)

如果消息隊(duì)列中有消息,并且當(dāng)前時(shí)候大于等于消息中的執(zhí)行時(shí)間,那么就直接返回這個(gè)消息給looper.loop消息處理,否則的話就要等待到消息的執(zhí)行時(shí)間:

[java] view plaincopynextPollTimeoutMillis = (int) Math.min(when - now, Integer.MAX_VALUE);

如果消息隊(duì)列中沒(méi)有消息,那就要進(jìn)入無(wú)窮等待狀態(tài)直到有新消息了:

[java] view plaincopynextPollTimeoutMillis = -1;

-1表示下次調(diào)用nativePollOnce時(shí),如果消息中沒(méi)有消息,就進(jìn)入無(wú)限等待狀態(tài)中去。

當(dāng)前nativePollOnce返回后,就去看看消息隊(duì)列中有沒(méi)有消息:

 
 
  1. [java] view plaincopyfinal Message msg = mMessages; 
  2. if (msg != null) { 
  3. final long when = msg.when; 
  4. if (now >= when) { 
  5. mBlocked = false; 
  6. mMessages = msg.next; 
  7. msg.next = null; 
  8. if (Config.LOGV) Log.v("MessageQueue", "Returning message: " + msg); 
  9. return msg; 
  10. } else { 
  11. nextPollTimeoutMillis = (int) Math.min(when - now, Integer.MAX_VALUE); 
  12. } else { 
  13. nextPollTimeoutMillis = -1; 
  14. }

這里計(jì)算出來(lái)的等待時(shí)間都是在下次調(diào)用nativePollOnce時(shí)使用的。

這里說(shuō)的等待,是空閑等待,而不是忙等待,因此,在進(jìn)入空閑等待狀態(tài)前,如果應(yīng)用程序注冊(cè)了Idlehandler接口來(lái)處理一些事情,那么就會(huì)先執(zhí) 行這里IdleHandler,然后再進(jìn)入等待狀態(tài)。IdlerHandler是定義在MessageQueue的一個(gè)內(nèi)部類(lèi):

 
 
  1. [java] view plaincopypublic class MessageQueue { 
  2. ...... 
  3. /** 
  4. * Callback interface for discovering when a thread is going to block 
  5. * waiting for more messages. 
  6. */ 
  7. public static interface IdleHandler { 
  8. /** 
  9. * Called when the message queue has run out of messages and will now 
  10. * wait for more. Return true to keep your idle handler active, false 
  11. * to have it removed. This may be called if there are still messages 
  12. * pending in the queue, but they are all scheduled to be dispatched 
  13. * after the current time. 
  14. */ 
  15. boolean queueIdle(); 
  16. ...... 

它只有一個(gè)成員函數(shù)queueIdle,執(zhí)行這個(gè)函數(shù)時(shí),如果返回值為false,那么就會(huì)從應(yīng)用程序中移除這個(gè)IdleHandler,否則的話就會(huì)在 應(yīng)用程序中繼續(xù)維護(hù)著這個(gè)IdleHandler,下次空閑時(shí)仍會(huì)再執(zhí)會(huì)這個(gè)IdleHandler。MessageQueue提供了 addIdleHandler和removeIdleHandler兩注冊(cè)和刪除IdleHandler。

回到MessageQueue函數(shù)中,它接下來(lái)就是在進(jìn)入等待狀態(tài)前,看看有沒(méi)有IdleHandler是需要執(zhí)行的:

 
 
  1.   [java] view plaincopy// If first time, then get the number of idlers to 
  2. run. 
  3.   if (pendingIdleHandlerCount < 0) { 
  4.   pendingIdleHandlerCount = mIdleHandlers.size(); 
  5.   } 
  6.   if (pendingIdleHandlerCount == 0) { 
  7.   // No idle handlers to run. Loop and wait some more. 
  8.   mBlocked = true; 
  9.   continue; 
  10.   } 
  11.   if (mPendingIdleHandlers == null) { 
  12.   mPendingIdleHandlers = new IdleHandler[Math.max(pendingIdleHandlerCount, 
  13. 4)]; 
  14.   } 
  15.   mPendingIdleHandlers = mIdleHandlers.toArray(mPendingIdleHandlers); 

如果沒(méi)有,即pendingIdleHandlerCount等于0,那下面的邏輯就不執(zhí)行了,通過(guò)continue語(yǔ)句直接進(jìn)入下一次循環(huán),否則就要把 注冊(cè)在mIdleHandlers中的IdleHandler取出來(lái),放在mPendingIdleHandlers數(shù)組中去。

接下來(lái)就是執(zhí)行這些注冊(cè)了的IdleHanlder了:

 
 
  1. [java] view plaincopy// Run the idle handlers. 
  2. // We only ever reach this code block during the first iteration. 
  3. for (int i = 0; i < pendingIdleHandlerCount; i++) { 
  4. final IdleHandler idler = mPendingIdleHandlers[i]; 
  5. mPendingIdleHandlers[i] = null; // release the reference to the handler 
  6. boolean keep = false; 
  7. try { 
  8. keep = idler.queueIdle(); 
  9. } catch (Throwable t) { 
  10. Log.wtf("MessageQueue", "IdleHandler threw exception", t); 

網(wǎng)頁(yè)標(biāo)題:Android應(yīng)用程序消息處理機(jī)制(Looper、Handler)分析(7)
標(biāo)題路徑:http://www.dlmjj.cn/article/dpiecdc.html