android应用程序消息处理机制(looper、handler)分析

上传人:xzh****18 文档编号:35367588 上传时间:2018-03-14 格式:DOCX 页数:29 大小:155.99KB
返回 下载 相关 举报
android应用程序消息处理机制(looper、handler)分析_第1页
第1页 / 共29页
android应用程序消息处理机制(looper、handler)分析_第2页
第2页 / 共29页
android应用程序消息处理机制(looper、handler)分析_第3页
第3页 / 共29页
android应用程序消息处理机制(looper、handler)分析_第4页
第4页 / 共29页
android应用程序消息处理机制(looper、handler)分析_第5页
第5页 / 共29页
点击查看更多>>
资源描述

《android应用程序消息处理机制(looper、handler)分析》由会员分享,可在线阅读,更多相关《android应用程序消息处理机制(looper、handler)分析(29页珍藏版)》请在金锄头文库上搜索。

1、 Android 应用程序是通过消息来驱动的,系统为每一个应用程序维护一个消息队例,应用程序的主线程不断地从这个消息队例中获取消息(Looper),然后对这些消息进行处理(Handler),这样就实现了通过消息来驱动应用程序的执行,本文将详细分析 Android 应用程序的消息处理机制。前面我们学习 Android 应用程序中的 Activity 启动(Android 应用程序启动过程源代码分析和Android 应用程序内部启动 Activity 过程(startActivity)的源代码分析)、Service 启动(Android 系统在新进程中启动自定义服务过程(startService)

2、的原理分析和 Android 应用程序绑定服务(bindService)的过程源代码分析)以及广播发送(Android 应用程序发送广播(sendBroadcast)的过程分析)时,它们都有一个共同的特点,当 ActivityManagerService 需要与应用程序进行并互时,如加载 Activity 和Service、处理广播待,会通过 Binder 进程间通信机制来知会应用程序,应用程序接收到这个请求时,它不是马上就处理这个请求,而是将这个请求封装成一个消息,然后把这个消息放在应用程序的消息队列中去,然后再通过消息循环来处理这个消息。这样做的好处就是消息的发送方只要把消息发送到应用程序

3、的消息队列中去就行了,它可以马上返回去处理别的事情,而不需要等待消息的接收方去处理完这个消息才返回,这样就可以提高系统的并发性。实质上,这就是一种异步处理机制。这样说可能还是比较笼统,我们以 Android 应用程序启动过程源代码分析一文中所介绍的应用程序启动过程的一个片断来具体看看是如何这种消息处理机制的。在这篇文章中,要启动的应用程序称为Activity,它的默认 Activity 是 MainActivity,它是由 Launcher 来负责启动的,而 Launcher 又是通过ActivityManagerService 来启动的,当 ActivityManagerService 为这

4、个即将要启的应用程序准备好新的进程后,便通过一个 Binder 进程间通信过程来通知这个新的进程来加载 MainActivity,如下图所示:它对应 Android 应用程序启动过程中的 Step 30 到 Step 35,有兴趣的读者可以回过头去参考Android 应用程序启动过程源代码分析一文。这里的 Step 30 中的 scheduleLaunchActivity 是ActivityManagerService 通过 Binder 进程间通信机制发送过来的请求,它请求应用程序中的ActivityThread 执行 Step 34 中的 performLaunchActivity 操作,

5、即启动 MainActivity 的操作。这里我们就可以看到,Step 30 的这个请求并没有等待 Step 34 这个操作完成就返回了,它只是把这个请求封装成一个消息,然后通过 Step 31 中的 queueOrSendMessage 操作把这个消息放到应用程序的消息队列中,然后就返回了。应用程序发现消息队列中有消息时,就会通过 Step 32 中的 handleMessage 操作来处理这个消息,即调用 Step 33 中的 handleLaunchActivity 来执行实际的加载 MainAcitivy 类的操作。了解 Android 应用程序的消息处理过程之后,我们就开始分样它的实

6、现原理了。与 Windows 应用程序的消息处理过程一样,Android 应用程序的消息处理机制也是由消息循环、消息发送和消息处理这三个部分组成的,接下来,我们就详细描述这三个过程。1. 消息循环在消息处理机制中,消息都是存放在一个消息队列中去,而应用程序的主线程就是围绕这个消息队列进入一个无限循环的,直到应用程序退出。如果队列中有消息,应用程序的主线程就会把它取出来,并分发给相应的 Handler 进行处理;如果队列中没有消息,应用程序的主线程就会进入空闲等待状态,等待下一个消息的到来。在 Android 应用程序中,这个消息循环过程是由 Looper 类来实现的,它定义在framework

7、s/base/core/java/android/os/Looper.java 文件中,在分析这个类之前,我们先看一下 Android应用程序主线程是如何进入到这个消息循环中去的。在 Android 应用程序进程启动过程的源代码分析一文中,我们分析了 Android 应用程序进程的启动过程,Android 应用程序进程在启动的时候,会在进程中加载 ActivityThread 类,并且执行这个类的 main函数,应用程序的消息循环过程就是在这个 main 函数里面实现的,我们来看看这个函数的实现,它定义在 frameworks/base/core/java/android/app/Activi

8、tyThread.java 文件中:view plain1.public final class ActivityThread 2. . 3. 4. public static final void main(String args) 5. . 6. 7. Looper.prepareMainLooper(); 8. 9. . 10. 11. ActivityThread thread = new ActivityThread(); 12. thread.attach(false); 13. 14. . 15. 16. Looper.loop(); 17. 18. . 19. 20. thre

9、ad.detach(); 21. 22. . 23. 24. 这个函数做了两件事情,一是在主线程中创建了一个 ActivityThread 实例,二是通过 Looper 类使主线程进入消息循环中,这里我们只关注后者。首先看 Looper.prepareMainLooper 函数的实现,这是一个静态成员函数,定义在frameworks/base/core/java/android/os/Looper.java 文件中:view plain1.public class Looper 2. . 3. 4. private static final ThreadLocal sThreadLocal =

10、 new ThreadLocal(); 5. 6. final MessageQueue mQueue; 7. 8. . 9. 10. /* Initialize the current thread as a looper. 11. * This gives you a chance to create handlers that then reference 12. * this looper, before actually starting the loop. Be sure to call 13. * link #loop() after calling this method, a

11、nd end it by calling 14. * link #quit(). 15. */ 16. public static final void prepare() 17. if (sThreadLocal.get() != null) 18. throw new RuntimeException(“Only one Looper may be created per thread“); 19. 20. sThreadLocal.set(new Looper(); 21. 22. 23. /* Initialize the current thread as a looper, mar

12、king it as an applications main 24. * looper. The main looper for your application is created by the Android environment, 25. * so you should never need to call this function yourself. 26. * link #prepare() 27. */ 28. 29. public static final void prepareMainLooper() 30. prepare(); 31. setMainLooper(

13、myLooper(); 32. if (Process.supportsProcesses() 33. myLooper().mQueue.mQuitAllowed = false; 34. 35. 36. 37. private synchronized static void setMainLooper(Looper looper) 38. mMainLooper = looper; 39. 40. 41. /* 42. * Return the Looper object associated with the current thread. Returns 43. * null if

14、the calling thread is not associated with a Looper. 44. */ 45. public static final Looper myLooper() 46. return (Looper)sThreadLocal.get(); 47. 48. 49. private Looper() 50. mQueue = new MessageQueue(); 51. mRun = true; 52. mThread = Thread.currentThread(); 53. 54. 55. . 56. 函数 prepareMainLooper 做的事情

15、其实就是在线程中创建一个 Looper 对象,这个 Looper 对象是存放在 sThreadLocal 成员变量里面的,成员变量 sThreadLocal 的类型为 ThreadLocal,表示这是一个线程局部变量,即保证每一个调用了 prepareMainLooper 函数的线程里面都有一个独立的 Looper 对象。在线程是创建 Looper 对象的工作是由 prepare 函数来完成的,而在创建 Looper 对象的时候,会同时创建一个消息队列 MessageQueue,保存在 Looper 的成员变量 mQueue 中,后续消息就是存放在这个队列中去。消息队列在 Android 应用程序消息处理机制中最重要的组件,因此,我们看看它的创建过程,即它的构造函数的实现

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > IT计算机/网络 > 多媒体应用

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号