Android應用程式啟動過程——Launcher源碼分析

來源:互聯網
上載者:User

標籤:android   應用程式啟動流程   

當我們在Launcher介面單擊一個應用程式圖示時就會啟動一個程式,那這一個過程究竟發生了些哪樣呢?讓我們跟蹤Launcher源碼來分析一下吧。

先上流程圖:

650) this.width=650;" src="http://img.blog.csdn.net/20140509134103500?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQva2luYm9z/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="SouthEast" />

step1、追蹤Launcher  從源碼中我們可以發現Launcher其實也是一個程式,它繼承於Activity。找到該檔案中的onCreate()方法,程式碼片段如下:


protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        LauncherApplication app = ((LauncherApplication)getApplication());        mModel = app.setLauncher(this);        mIconCache = app.getIconCache();        mDragController = new DragController(this);        mInflater = getLayoutInflater();        mAppWidgetManager = AppWidgetManager.getInstance(this);        mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);        mAppWidgetHost.startListening();        if (PROFILE_STARTUP) {            android.os.Debug.startMethodTracing("/sdcard/launcher");        }        loadHotseats();        checkForLocaleChange();        setWallpaperDimension();        setContentView(R.layout.launcher);        setupViews();        registerContentObservers();        lockAllApps();        mSavedState = savedInstanceState;        restoreState(mSavedState);        if (PROFILE_STARTUP) {            android.os.Debug.stopMethodTracing();        }        if (!mRestoring) {            mModel.startLoader(this, true);        }

找到關鍵區段,也就是上面被標註紅色代碼。mModel是LauncherModel類型對象,接下來我們跟蹤LauncerModel.java檔案。



step2、  `LauncherModel是一個BroadcastReceiver,在這裡我們先進入startLoader(Context context, boolean isLaunching) 方法。

650) this.width=650;" src="http://img.blog.csdn.net/20140509135622343?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQva2luYm9z/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="SouthEast" />

LoaderTask實現了Runnable介面,接著我們再找到該實作類別的run()方法,程式碼片段如下:

650) this.width=650;" src="http://img.blog.csdn.net/20140509140121828?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQva2luYm9z/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="SouthEast" />

讀取系統中各應用主Activity的資訊程式碼片段如下:

650) this.width=650;" src="http://img.blog.csdn.net/20140509140637968?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQva2luYm9z/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="SouthEast" />

step3、當我們單擊應用程式圖示時所執行的對應方法為Launcher.java裡的onClick()方法,其代碼如下:

650) this.width=650;" src="http://img.blog.csdn.net/20140509141017421?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQva2luYm9z/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="SouthEast" />650) this.width=650;" src="http://img.blog.csdn.net/20140509141234218?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQva2luYm9z/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="SouthEast" />

到此就對應用程式的啟動過程分析結束了,歡迎各位網友的指正和建議,以其共同進步。


本文出自 “嚴富坤-編程技術” 部落格,請務必保留此出處http://kinbos.blog.51cto.com/2092114/1408865

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.