本篇內(nèi)容介紹了“Android應用程序啟動是怎么實現(xiàn)的”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!

成都創(chuàng)新互聯(lián)公司是一家專業(yè)從事網(wǎng)站建設、網(wǎng)絡營銷、小程序定制開發(fā)、網(wǎng)站運營為一體的建站企業(yè);在網(wǎng)站建設告別千篇一律,告別似曾相識,這一次我們重新定義網(wǎng)站建設,讓您的網(wǎng)站別具一格。自適應網(wǎng)站建設,實現(xiàn)全網(wǎng)營銷!一站適應多終端,一樣的建站,不一樣的體驗!
Android應用程序進程在啟動的時候, 會在進程中加載ActivityThread類,并且執(zhí)行這個類的main函數(shù)。
應用程序的消息循環(huán)過程就是在這個main函數(shù)里面實現(xiàn)的。
我們來看看這個函數(shù)的實現(xiàn),它定義在frameworks/base/core/java/android/app/ActivityThread.java文件中:
[java] view plaincopypublic final class ActivityThread { ...... public static final void main(String[] args) { ...... Looper.prepareMainLooper(); ...... ActivityThread thread = new ActivityThread(); thread.attach(false); ...... Looper.loop(); ...... thread.detach(); ...... } }這個函數(shù)做了兩件事情,一是在主線程中創(chuàng)建了一個ActivityThread實例,二是通過Looper類使主線程進入消息循環(huán)中,這里我們只關注后者。
首先看Looper.prepareMainLooper函數(shù)的實現(xiàn),這是一個靜態(tài)成員函數(shù),定義在frameworks/base/core/java/android/os/Looper.java文件中:
[java] view plaincopypublic class Looper { ...... private static final ThreadLocal sThreadLocal = new ThreadLocal(); final MessageQueue mQueue; ...... /** Initialize the current thread as a looper. * This gives you a chance to create handlers that then reference * this looper, before actually starting the loop. Be sure to call * {@link #loop()} after calling this method, and end it by calling * {@link #quit()}. */ public static final void prepare() { if (sThreadLocal.get() != null) { throw new RuntimeException("Only one Looper may be created per thread"); } sThreadLocal.set(new Looper()); } /** Initialize the current thread as a looper, marking it as an application's main * looper. The main looper for your application is created by the Android environment, * so you should never need to call this function yourself. * {@link #prepare()} */ public static final void prepareMainLooper() { prepare(); setMainLooper(myLooper()); if (Process.supportsProcesses()) { myLooper().mQueue.mQuitAllowed = false; } } private synchronized static void setMainLooper(Looper looper) { mMainLooper = looper; } /** * Return the Looper object associated with the current thread. Returns * null if the calling thread is not associated with a Looper. */ public static final Looper myLooper() { return (Looper)sThreadLocal.get(); } private Looper() { mQueue = new MessageQueue(); mRun = true; mThread = Thread.currentThread(); } ...... }“Android應用程序啟動是怎么實現(xiàn)的”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關的知識可以關注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!
本文名稱:Android應用程序啟動是怎么實現(xiàn)的
URL分享:http://chinadenli.net/article34/ggpdse.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、外貿(mào)網(wǎng)站建設、網(wǎng)站設計公司、網(wǎng)站收錄、定制網(wǎng)站、面包屑導航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)