当前位置:首页 >娱乐 >Android应用程序消息处理机制(Looper、Handler)分析(8) 理机就不设置超时时间了

Android应用程序消息处理机制(Looper、Handler)分析(8) 理机就不设置超时时间了

2024-06-28 07:11:17 [百科] 来源:避面尹邢网

Android应用程序消息处理机制(Looper、应用程Handler)分析(8)

作者:佚名 移动开发 Android 执行完这些IdleHandler之后,序消息处线程下次调用nativePollOnce函数时,理机就不设置超时时间了,分析因为,应用程很有可能在执行IdleHandler的序消息处时候,已经有新的理机消息加入到消息队列中去了。

执行完这些IdleHandler之后,分析线程下次调用nativePollOnce函数时,应用程就不设置超时时间了。序消息处

因为,理机很有可能在执行IdleHandler的分析时候,已经有新的应用程消息加入到消息队列中去了。

Android应用程序消息处理机制(Looper、Handler)分析(8) 理机就不设置超时时间了

正因为如此,序消息处

Android应用程序消息处理机制(Looper、Handler)分析(8) 理机就不设置超时时间了

要重置nextPollTimeoutMillis的理机值:

Android应用程序消息处理机制(Looper、Handler)分析(8) 理机就不设置超时时间了

  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的这个next函数之后,我们就要深入分析一下JNI方法nativePollOnce了,看看它是如何进入等待状态的, 这个函数定义在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.   } 

这个函数首先是通过传进入的参数ptr取回前面在Java层创建MessageQueue对象时在JNI层创建的NatvieMessageQueue对象,然后调用它的pollOnce函数:

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

这里将操作转发给mLooper对象的pollOnce函数处理,这里的mLooper对象是在C++层的对象,它也是在前面在JNI层创建的 NatvieMessageQueue对象时创建的,它的pollOnce函数定义在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); 

为了方便讨论,我们把这个函数的无关部分都去掉,它主要就是调用pollInner函数来进一步操作,如果pollInner返回值不等于0,这个函数就可以返回了。

函数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; 

 

责任编辑:闫佳明 来源: bbs.9ria Android消息处理机制LooperHandler

(责任编辑:焦点)

    推荐文章
    热点阅读