日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
Android應(yīng)用程序消息處理機(jī)制(Looper、Handler)分析(2)

在Android應(yīng)用程序進(jìn)程啟動(dòng)過(guò)程的源代碼分析一文中,我們分析了Android應(yīng)用程序進(jìn)程的啟動(dòng)過(guò)程。

為巴州等地區(qū)用戶提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及巴州網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)、巴州網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!

Android應(yīng)用程序進(jìn)程在啟動(dòng)的時(shí)候, 會(huì)在進(jìn)程中加載ActivityThread類,并且執(zhí)行這個(gè)類的main函數(shù)。

應(yīng)用程序的消息循環(huán)過(guò)程就是在這個(gè)main函數(shù)里面實(shí)現(xiàn)的。

我們來(lái)看看這個(gè)函數(shù)的實(shí)現(xiàn),它定義在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.   } 

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

首先看Looper.prepareMainLooper函數(shù)的實(shí)現(xiàn),這是一個(gè)靜態(tài)成員函數(shù),定義在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.   } 

網(wǎng)頁(yè)名稱:Android應(yīng)用程序消息處理機(jī)制(Looper、Handler)分析(2)
網(wǎng)頁(yè)網(wǎng)址:http://www.5511xx.com/article/coedjpg.html