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

移动开发 Android
在Android应用程序进程启动过程的源代码分析一文中,我们分析了Android应用程序进程的启动过程。Android应用程序进程在启动的时候, 会在进程中加载ActivityThread类,并且执行这个类的main函数。应用程序的消息循环过程就是在这个main函数里面实现的。

在Android应用程序进程启动过程的源代码分析一文中,我们分析了Android应用程序进程的启动过程。

Android应用程序进程在启动的时候, 会在进程中加载ActivityThread类,并且执行这个类的main函数。

应用程序的消息循环过程就是在这个main函数里面实现的。

我们来看看这个函数的实现,它定义在frameworks/base/core/java/android/app/ActivityThread.java文件中:

  1. [java] view plaincopypublic final class ActivityThread { 
  2.   ...... 
  3.   public static final void main(String[] args) { 
  4.   ...... 
  5.   Looper.prepareMainLooper(); 
  6.   ...... 
  7.   ActivityThread thread = new ActivityThread(); 
  8.   thread.attach(false); 
  9.   ...... 
  10.   Looper.loop(); 
  11.   ...... 
  12.   thread.detach(); 
  13.   ...... 
  14.   } 
  15.   } 

这个函数做了两件事情,一是在主线程中创建了一个ActivityThread实例,二是通过Looper类使主线程进入消息循环中,这里我们只关注后者。

首先看Looper.prepareMainLooper函数的实现,这是一个静态成员函数,定义在frameworks/base/core/java/android/os/Looper.java文件中:

  1.  [java] view plaincopypublic class Looper { 
  2.   ...... 
  3.   private static final ThreadLocal sThreadLocal = new ThreadLocal(); 
  4.   final MessageQueue mQueue; 
  5.   ...... 
  6.   /** Initialize the current thread as a looper. 
  7.   * This gives you a chance to create handlers that then reference 
  8.   * this looper, before actually starting the loop. Be sure to call 
  9.   * {@link #loop()} after calling this method, and end it by calling 
  10.   * {@link #quit()}. 
  11.   */ 
  12.   public static final void prepare() { 
  13.   if (sThreadLocal.get() != null) { 
  14.   throw new RuntimeException("Only one Looper may be created per 
  15. thread"); 
  16.   } 
  17.   sThreadLocal.set(new Looper()); 
  18.   } 
  19.   /** Initialize the current thread as a looper, marking it as an 
  20. application's main 
  21.   * looper. The main looper for your application is created by the Android 
  22. environment, 
  23.   * so you should never need to call this function yourself. 
  24.   * {@link #prepare()} 
  25.   */ 
  26.   public static final void prepareMainLooper() { 
  27.   prepare(); 
  28.   setMainLooper(myLooper()); 
  29.   if (Process.supportsProcesses()) { 
  30.   myLooper().mQueue.mQuitAllowed = false
  31.   } 
  32.   } 
  33.   private synchronized static void setMainLooper(Looper looper) { 
  34.   mMainLooper = looper; 
  35.   } 
  36.   /** 
  37.   * Return the Looper object associated with the current thread. Returns 
  38.   * null if the calling thread is not associated with a Looper. 
  39.   */ 
  40.   public static final Looper myLooper() { 
  41.   return (Looper)sThreadLocal.get(); 
  42.   } 
  43.   private Looper() { 
  44.   mQueue = new MessageQueue(); 
  45.   mRun = true
  46.   mThread = Thread.currentThread(); 
  47.   } 
  48.   ...... 
  49.   } 

 

责任编辑:闫佳明 来源: bbs.9ria
相关推荐

2014-05-22 15:07:44

Android消息处理机制Looper

2014-05-22 15:48:50

Android消息处理机制Looper

2014-05-22 15:04:00

Android消息处理机制Looper

2014-05-22 15:38:27

Android消息处理机制Looper

2014-05-22 15:41:59

Android消息处理机制Looper

2014-05-22 14:57:28

Android消息处理机制Looper

2014-05-22 15:15:53

Android消息处理机制Looper

2014-05-22 15:18:25

Android消息处理机制Looper

2014-05-22 15:33:31

Android消息处理机制Looper

2014-05-22 15:45:58

Android消息处理机制Looper

2011-04-28 11:01:40

Android消息处理LooperHandler

2011-11-23 09:33:45

HandlerLooperMessage

2014-05-27 10:13:57

移动技术半月刊

2016-10-21 13:03:18

androidhandlerlooper

2011-09-05 17:40:40

MTK定时器

2021-08-12 16:28:10

AndroidHandleLooper

2011-03-17 09:20:05

异常处理机制

2019-07-25 13:13:25

AndroidHandler消费机制

2023-06-15 14:09:00

解析器Servlet容器

2023-03-08 08:54:59

SpringMVCJava
点赞
收藏

51CTO技术栈公众号