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

如果消息队列中有消息,并且当前时候大于等于消息中的执行时间,那么就直接返回这个消息给looper.loop消息处理,否则的话就要等待到消息的执行时间:

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

如果消息队列中没有消息,那就要进入无穷等待状态直到有新消息了:

[java] view plaincopynextPollTimeoutMillis = -1;

-1表示下次调用nativePollOnce时,如果消息中没有消息,就进入无限等待状态中去。

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

这里计算出来的等待时间都是在下次调用nativePollOnce时使用的。

这里说的等待,是空闲等待,而不是忙等待,因此,在进入空闲等待状态前,如果应用程序注册了Idlehandler接口来处理一些事情,那么就会先执 行这里IdleHandler,然后再进入等待状态。IdlerHandler是定义在MessageQueue的一个内部类:

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

它只有一个成员函数queueIdle,执行这个函数时,如果返回值为false,那么就会从应用程序中移除这个IdleHandler,否则的话就会在 应用程序中继续维护着这个IdleHandler,下次空闲时仍会再执会这个IdleHandler。MessageQueue提供了 addIdleHandler和removeIdleHandler两注册和删除IdleHandler。

回到MessageQueue函数中,它接下来就是在进入等待状态前,看看有没有IdleHandler是需要执行的:

 
 
  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,那下面的逻辑就不执行了,通过continue语句直接进入下一次循环,否则就要把 注册在mIdleHandlers中的IdleHandler取出来,放在mPendingIdleHandlers数组中去。

接下来就是执行这些注册了的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)
网页URL:http://www.shufengxianlan.com/qtweb/news1/374451.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联