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

应用程序的主线程准备就好消息队列并且进入到消息循环后,其它地方就可以往这个消息队列中发送消息了。

我们继续以文章开始介绍的Android应用程序启动过程源代码分析一文中的应用程序启动过为例,说明应用程序是如何把消息加入到应用程序的消息队列中去的。

在Android应用程序启动过程源代码分析这篇文章的Step 30中,ActivityManagerService通过调用ApplicationThread类的scheduleLaunchActivity函 数通知应用程序。

它可以加载应用程序的默认Activity了,这个函数定义在frameworks/base/core/java/android /app/ActivityThread.java文件中:

 
 
  1.   [java] view plaincopypublic final class ActivityThread { 
  2.   ...... 
  3.   private final class ApplicationThread extends ApplicationThreadNative { 
  4.   ...... 
  5.   // we use token to identify this activity without having to send the 
  6.   // activity itself back to the activity manager. (matters more with 
  7. ipc) 
  8.   public final void scheduleLaunchActivity(Intent intent, IBinder token, int 
  9. ident, 
  10.   ActivityInfo info, Bundle state, List pendingResults, 
  11.   List pendingNewIntents, boolean notResumed, boolean isForward) 
  12.   ActivityClientRecord r = new ActivityClientRecord(); 
  13.   r.token = token; 
  14.   r.ident = ident; 
  15.   r.intent = intent; 
  16.   r.activityInfo = info; 
  17.   r.state = state; 
  18.   r.pendingResults = pendingResults; 
  19.   r.pendingIntents = pendingNewIntents; 
  20.   r.startsNotResumed = notResumed; 
  21.   r.isForward = isForward; 
  22.   queueOrSendMessage(H.LAUNCH_ACTIVITY, r); 
  23.   } 
  24.   ...... 
  25.   } 
  26.   ...... 
  27.   } 

这里把相关的参数都封装成一个ActivityClientRecord对象r,然后调用queueOrSendMessage函数来往应用程序的消息队 列中加入一个新的消息(H.LAUNCH_ACTIVITY),这个函数定义在frameworks/base/core/java/android /app/ActivityThread.java文件中:

 
 
  1. [java] view plaincopypublic final class ActivityThread { 
  2. ...... 
  3. private final class ApplicationThread extends ApplicationThreadNative { 
  4. ...... 
  5. // if the thread hasn't started yet, we don't have the handler, so just 
  6. // save the messages until we're ready. 
  7. private final void queueOrSendMessage(int what, Object obj) { 
  8. queueOrSendMessage(what, obj, 0, 0); 
  9. ...... 
  10. private final void queueOrSendMessage(int what, Object obj, int arg1, int 
  11. g2) { 
  12. synchronized (this) { 
  13. ...... 
  14. Message msg = Message.obtain(); 
  15. msg.what = what; 
  16. msg.obj = obj; 
  17. msg.arg1 = arg1; 
  18. msg.arg2 = arg2; 
  19. mH.sendMessage(msg); 
  20. ...... 
  21. ...... 

文章名称:Android应用程序消息处理机制(Looper、Handler)分析(10)
标题链接:http://www.shufengxianlan.com/qtweb/news5/341655.html

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

广告

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