av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

Android應用程序消息處理機制(Looper、Handler)分析(7)

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

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

如果消息隊列中沒有消息,那就要進入無窮等待狀態(tài)直到有新消息了:

[java] view plaincopynextPollTimeoutMillis = -1;

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

當前nativePollOnce返回后,就去看看消息隊列中有沒有消息:

 
 
  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. }

這里計算出來的等待時間都是在下次調(diào)用nativePollOnce時使用的。

這里說的等待,是空閑等待,而不是忙等待,因此,在進入空閑等待狀態(tài)前,如果應用程序注冊了Idlehandler接口來處理一些事情,那么就會先執(zhí) 行這里IdleHandler,然后再進入等待狀態(tài)。IdlerHandler是定義在MessageQueue的一個內(nè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. ...... 

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

回到MessageQueue函數(shù)中,它接下來就是在進入等待狀態(tà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); 

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

接下來就是執(zhí)行這些注冊了的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); 

分享題目:Android應用程序消息處理機制(Looper、Handler)分析(7)
標題路徑:http://uogjgqi.cn/article/dpiecdc.html
掃二維碼與項目經(jīng)理溝通

我們在微信上24小時期待你的聲音

解答本文疑問/技術咨詢/運營咨詢/技術建議/互聯(lián)網(wǎng)交流