自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Android應(yīng)用程序消息處理機(jī)制(Looper、Handler)分析(8)

移動(dòng)開發(fā) Android
執(zhí)行完這些IdleHandler之后,線程下次調(diào)用nativePollOnce函數(shù)時(shí),就不設(shè)置超時(shí)時(shí)間了,因?yàn)?,很有可能在?zhí)行IdleHandler的時(shí)候,已經(jīng)有新的消息加入到消息隊(duì)列中去了。

執(zhí)行完這些IdleHandler之后,線程下次調(diào)用nativePollOnce函數(shù)時(shí),就不設(shè)置超時(shí)時(shí)間了。

因?yàn)?,很有可能在?zhí)行IdleHandler的時(shí)候,已經(jīng)有新的消息加入到消息隊(duì)列中去了。

正因?yàn)槿绱耍?/p>

要重置nextPollTimeoutMillis的值:

  1. [java] view plaincopy// While calling an idle handler, a new message could 
  2. ve been delivered 
  3. // so go back and look again for a pending message without waiting. 
  4. nextPollTimeoutMillis = 0

分析完MessageQueue的這個(gè)next函數(shù)之后,我們就要深入分析一下JNI方法nativePollOnce了,看看它是如何進(jìn)入等待狀態(tài)的, 這個(gè)函數(shù)定義在frameworks/base/core/jni/android_os_MessageQueue.cpp文件中:

  1.   [cpp] view plaincopystatic void 
  2. android_os_MessageQueue_nativePollOnce(JNIEnv* env, jobject obj, 
  3.   jint ptr, jint timeoutMillis) { 
  4.   NativeMessageQueue* nativeMessageQueue = 
  5. reinterpret_cast(ptr); 
  6.   nativeMessageQueue->pollOnce(timeoutMillis); 
  7.   } 

這個(gè)函數(shù)首先是通過(guò)傳進(jìn)入的參數(shù)ptr取回前面在Java層創(chuàng)建MessageQueue對(duì)象時(shí)在JNI層創(chuàng)建的NatvieMessageQueue對(duì)象,然后調(diào)用它的pollOnce函數(shù):

  1. [cpp] view plaincopyvoid NativeMessageQueue::pollOnce(int timeoutMillis) 
  2.  
  3.  mLooper->pollOnce(timeoutMillis); 
  4.  } 

這里將操作轉(zhuǎn)發(fā)給mLooper對(duì)象的pollOnce函數(shù)處理,這里的mLooper對(duì)象是在C++層的對(duì)象,它也是在前面在JNI層創(chuàng)建的 NatvieMessageQueue對(duì)象時(shí)創(chuàng)建的,它的pollOnce函數(shù)定義在frameworks/base/libs/utils /Looper.cpp文件中:

  1. [cpp] view plaincopyint Looper::pollOnce(int timeoutMillis, int* outFd, 
  2. t* outEvents, void** outData) { 
  3. int result = 0
  4. for (;;) { 
  5. ...... 
  6. if (result != 0) { 
  7. ...... 
  8. return result; 
  9. result = pollInner(timeoutMillis); 

為了方便討論,我們把這個(gè)函數(shù)的無(wú)關(guān)部分都去掉,它主要就是調(diào)用pollInner函數(shù)來(lái)進(jìn)一步操作,如果pollInner返回值不等于0,這個(gè)函數(shù)就可以返回了。

函數(shù)pollInner的定義如下:

  1. [cpp] view plaincopyint Looper::pollInner(int timeoutMillis) { 
  2. ...... 
  3. int result = ALOOPER_POLL_WAKE; 
  4. ...... 
  5. #ifdef LOOPER_USES_EPOLL 
  6. struct epoll_event eventItems[EPOLL_MAX_EVENTS]; 
  7. int eventCount = epoll_wait(mEpollFd, eventItems, EPOLL_MAX_EVENTS, 
  8. meoutMillis); 
  9. bool acquiredLock = false
  10. #else 
  11. ...... 
  12. #endif 
  13. if (eventCount < 0) { 
  14. if (errno == EINTR) { 
  15. goto Done; 
  16. LOGW("Poll failed with an unexpected error, errno=%d", errno); 
  17. result = ALOOPER_POLL_ERROR; 
  18. goto Done; 
  19. if (eventCount == 0) { 
  20. ...... 
  21. result = ALOOPER_POLL_TIMEOUT; 
  22. goto Done; 
  23. ...... 
  24. #ifdef LOOPER_USES_EPOLL 
  25. for (int i = 0; i < eventCount; i++) { 
  26. int fd = eventItems[i].data.fd; 
  27. uint32_t epollEvents = eventItems[i].events; 
  28. if (fd == mWakeReadPipeFd) { 
  29. if (epollEvents & EPOLLIN) { 
  30. awoken(); 
  31. else { 
  32. LOGW("Ignoring unexpected epoll events 0x%x on wake read pipe."
  33. ollEvents); 
  34. else { 
  35. ...... 
  36. if (acquiredLock) { 
  37. mLock.unlock(); 
  38. Done: ; 
  39. #else 
  40. ...... 
  41. #endif 
  42. ...... 
  43. return result; 

 

責(zé)任編輯:閆佳明 來(lái)源: bbs.9ria
相關(guān)推薦

2014-05-22 15:41:59

Android消息處理機(jī)制Looper

2014-05-22 15:00:16

Android消息處理機(jī)制Looper

2014-05-22 15:04:00

Android消息處理機(jī)制Looper

2014-05-22 15:07:44

Android消息處理機(jī)制Looper

2014-05-22 15:48:50

Android消息處理機(jī)制Looper

2014-05-22 14:57:28

Android消息處理機(jī)制Looper

2014-05-22 15:15:53

Android消息處理機(jī)制Looper

2014-05-22 15:18:25

Android消息處理機(jī)制Looper

2014-05-22 15:33:31

Android消息處理機(jī)制Looper

2014-05-22 15:45:58

Android消息處理機(jī)制Looper

2011-04-28 11:01:40

Android消息處理LooperHandler

2011-11-23 09:33:45

HandlerLooperMessage

2014-05-27 10:13:57

移動(dòng)技術(shù)半月刊

2016-10-21 13:03:18

androidhandlerlooper

2011-09-05 17:40:40

MTK定時(shí)器

2011-03-17 09:20:05

異常處理機(jī)制

2021-08-12 16:28:10

AndroidHandleLooper

2023-03-08 08:54:59

SpringMVCJava

2023-06-15 14:09:00

解析器Servlet容器

2011-07-01 14:20:59

Qt 事件
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)