自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Android應用程序消息處理機制(Looper、Handler)分析(2)

移動開發(fā) Android
在Android應用程序進程啟動過程的源代碼分析一文中,我們分析了Android應用程序進程的啟動過程。Android應用程序進程在啟動的時候, 會在進程中加載ActivityThread類,并且執(zhí)行這個類的main函數。應用程序的消息循環(huán)過程就是在這個main函數里面實現的。

在Android應用程序進程啟動過程的源代碼分析一文中,我們分析了Android應用程序進程的啟動過程。

Android應用程序進程在啟動的時候, 會在進程中加載ActivityThread類,并且執(zhí)行這個類的main函數。

應用程序的消息循環(huán)過程就是在這個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.   } 

這個函數做了兩件事情,一是在主線程中創(chuàng)建了一個ActivityThread實例,二是通過Looper類使主線程進入消息循環(huán)中,這里我們只關注后者。

首先看Looper.prepareMainLooper函數的實現,這是一個靜態(tài)成員函數,定義在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:41:59

Android消息處理機制Looper

2014-05-22 15:38:27

Android消息處理機制Looper

2014-05-22 15:04:00

Android消息處理機制Looper

2014-05-22 15:07:44

Android消息處理機制Looper

2014-05-22 15:48:50

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定時器

2011-03-17 09:20:05

異常處理機制

2021-08-12 16:28:10

AndroidHandleLooper

2023-03-08 08:54:59

SpringMVCJava

2023-06-15 14:09:00

解析器Servlet容器

2011-07-01 14:14:34

Qt 事件
點贊
收藏

51CTO技術棧公眾號