標籤: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