【Android】應用程序啓動過程源碼分析

  在Android系統中,應用程序是由Activity組成的,所以,應用程序的啓動過程實際上就是應用程序中的默認Activity的啓動過程,本文將詳細分析應用程序框架層的源代碼,瞭解Android應用程序的啓動過程。
  啓動Android應用程序中的Activity的兩種情景:其中,在手機屏幕中點擊應用程序圖標的情景就會引起Android應用程序中的默認Activity的啓動,從而把應用程序啓動起來。這種啓動方式的特色是會啓動一個新的進程來加載相應的Activity。
  這裏,咱們以這個例子爲例來講明Android應用程序的啓動過程,即MainActivity的啓動過程。

Step 1. Launcher.startActivitySafely

  在Android系統中,應用程序是由Launcher啓動起來的,其實,Launcher自己也是一個應用程序,其它的應用程序安裝後,Launcher的界面上就會出現一個相應的圖標,點擊這個圖標時,Launcher就會對應的應用程序啓動起來。java

    Launcher的源代碼工程在packages/apps/Launcher2目錄下,負責啓動其它應用程序的源代碼實如今src/com/android/launcher2/Launcher.java文件中:android

/**
* Default launcher application.
*/
public final class Launcher extends Activity
        implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks, AllAppsView.Watcher {

    ......

    /**
    * Launches the intent referred by the clicked shortcut.
    *
    * @param v The view representing the clicked shortcut.
    */
    public void onClick(View v) {
        Object tag = v.getTag();
        if (tag instanceof ShortcutInfo) {
            // Open shortcut
            final Intent intent = ((ShortcutInfo) tag).intent;
            int[] pos = new int[2];
            v.getLocationOnScreen(pos);
            intent.setSourceBounds(new Rect(pos[0], pos[1],
                pos[0] + v.getWidth(), pos[1] + v.getHeight()));
            startActivitySafely(intent, tag);
        } else if (tag instanceof FolderInfo) {
            ......
        } else if (v == mHandleView) {
            ......
        }
    }

    void startActivitySafely(Intent intent, Object tag) {
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        try {
            startActivity(intent);
        } catch (ActivityNotFoundException e) {
            ......
        } catch (SecurityException e) {
            ......
        }
    }

    ......

}
View Code
    MainActivity在AndroidManifest.xml文件中是這樣配置的:
<activity android:name=".MainActivity"  
      android:label="@string/app_name">  
       <intent-filter>  
        <action android:name="android.intent.action.MAIN" />  
        <category android:name="android.intent.category.LAUNCHER" />  
    </intent-filter>  
</activity>  
View Code
    所以,這裏的intent包含的信息爲:action = "android.intent.action.Main",category="android.intent.category.LAUNCHER",表示它要啓動的Activity爲MainActivity。Intent.FLAG_ACTIVITY_NEW_TASK表示要在一個新的Task中啓動這個Activity,注意, Task是Android系統中的概念,它不一樣於進程Process的概念。簡單地說,一個Task是一系列Activity的集合,這個集合是以堆棧的形式來組織的,遵循後進先出的原則。

Step 2. Activity.startActivity

  在Step 1中,咱們看到,Launcher繼承於Activity類,而Activity類實現了startActivity函數,所以,這裏就調用了Activity.startActivity函數。app

  它實如今frameworks/base/core/java/android/app/Activity.java文件中:框架

public class Activity extends ContextThemeWrapper
        implements LayoutInflater.Factory,
        Window.Callback, KeyEvent.Callback,
        OnCreateContextMenuListener, ComponentCallbacks {

    ......

    @Override
    public void startActivity(Intent intent) {
        startActivityForResult(intent, -1);
    }

    ......

}
View Code

Step 3. Activity.startActivityForResult

  這個函數也是實如今frameworks/base/core/java/android/app/Activity.java文件中:
public class Activity extends ContextThemeWrapper
        implements LayoutInflater.Factory,
        Window.Callback, KeyEvent.Callback,
        OnCreateContextMenuListener, ComponentCallbacks {

    ......

    public void startActivityForResult(Intent intent, int requestCode) {
        if (mParent == null) {
            Instrumentation.ActivityResult ar =
                mInstrumentation.execStartActivity(
                this, mMainThread.getApplicationThread(), mToken, this,
                intent, requestCode);
            ......
        } else {
            ......
        }


    ......

}
View Code
  這裏的mInstrumentation是Activity類的成員變量,它的類型是Intrumentation,定義在frameworks/base/core/java/android/app/Instrumentation.java文件中,它用來 監控應用程序和系統的交互

  這裏的mMainThread也是Activity類的成員變量,它的類型是ActivityThread,它表明的是應用程序的主線程。這裏經過mMainThread.getApplicationThread得到它裏面的ApplicationThread成員變量,它是一個Binder對象,後面會看到,ActivityManagerService會使用它來和ActivityThread來進行進程間通訊。ide

  這裏咱們需注意的是,這裏的mMainThread表明的是Launcher應用程序運行的進程。函數

  這裏的mToken也是Activity類的成員變量,它是一個Binder對象的遠程接口。oop

Step 4. Instrumentation.execStartActivity    

  這個函數定義在frameworks/base/core/java/android/app/Instrumentation.java文件中:
public class Instrumentation {

    ......

    public ActivityResult execStartActivity(
    Context who, IBinder contextThread, IBinder token, Activity target,
    Intent intent, int requestCode) {
        IApplicationThread whoThread = (IApplicationThread) contextThread;
        if (mActivityMonitors != null) {
            ......
        }
        try {
            int result = ActivityManagerNative.getDefault()
                .startActivity(whoThread, intent,
                intent.resolveTypeIfNeeded(who.getContentResolver()),
                null, 0, token, target != null ? target.mEmbeddedID : null,
                requestCode, false, false);
            ......
        } catch (RemoteException e) {
        }
        return null;
    }

    ......

}
View Code

  這裏的ActivityManagerNative.getDefault返回ActivityManagerService的遠程接口,即ActivityManagerProxy接口ui

  這裏的intent.resolveTypeIfNeeded返回這個intent的MIME類型,在這個例子中,沒有AndroidManifest.xml設置MainActivity的MIME類型,所以,這裏返回null。

Step 5. ActivityManagerProxy.startActivity

  這個函數定義在frameworks/base/core/java/android/app/ActivityManagerNative.java文件中:
class ActivityManagerProxy implements IActivityManager
{

    ......

    public int startActivity(IApplicationThread caller, Intent intent,
            String resolvedType, Uri[] grantedUriPermissions, int grantedMode,
            IBinder resultTo, String resultWho,
            int requestCode, boolean onlyIfNeeded,
            boolean debug) throws RemoteException {
        Parcel data = Parcel.obtain();
        Parcel reply = Parcel.obtain();
        data.writeInterfaceToken(IActivityManager.descriptor);
        data.writeStrongBinder(caller != null ? caller.asBinder() : null);
        intent.writeToParcel(data, 0);
        data.writeString(resolvedType);
        data.writeTypedArray(grantedUriPermissions, 0);
        data.writeInt(grantedMode);
        data.writeStrongBinder(resultTo);
        data.writeString(resultWho);
        data.writeInt(requestCode);
        data.writeInt(onlyIfNeeded ? 1 : 0);
        data.writeInt(debug ? 1 : 0);
        mRemote.transact(START_ACTIVITY_TRANSACTION, data, reply, 0);
        reply.readException();
        int result = reply.readInt();
        reply.recycle();
        data.recycle();
        return result;
    }

    ......

}
View Code

  從上面的調用能夠知道,這裏的參數this

  1. resolvedType、grantedUriPermissions和resultWho均爲null;
  2. 參數caller爲ApplicationThread類型的Binder實體;
  3. 參數resultTo爲一個Binder實體的遠程接口,先不關注它;
  4. 參數grantedMode爲0,也先不關注它;
  5. 參數requestCode爲-1;
  6. 參數onlyIfNeeded和debug均空false。

Step 6. ActivityManagerService.startActivity

  上一步Step 5經過Binder驅動程序就進入到ActivityManagerService的startActivity函數來了,它定義在frameworks/base/services/java/com/android/server/am/ActivityManagerService.java文件中:
public final class ActivityManagerService extends ActivityManagerNative
        implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback {

    ......

    public final int startActivity(IApplicationThread caller,
            Intent intent, String resolvedType, Uri[] grantedUriPermissions,
            int grantedMode, IBinder resultTo,
            String resultWho, int requestCode, boolean onlyIfNeeded,
            boolean debug) {
        return mMainStack.startActivityMayWait(caller, intent, resolvedType,
            grantedUriPermissions, grantedMode, resultTo, resultWho,
            requestCode, onlyIfNeeded, debug, null, null);
    }


    ......

}
View Code

  這裏只是簡單地將操做轉發給成員變量mMainStack的startActivityMayWait函數。spa

  這裏的mMainStack的類型爲ActivityStack。

Step 7. ActivityStack.startActivityMayWait

  這個函數定義在frameworks/base/services/java/com/android/server/am/ActivityStack.java文件中:
public class ActivityStack {

    ......

    final int startActivityMayWait(IApplicationThread caller,
            Intent intent, String resolvedType, Uri[] grantedUriPermissions,
            int grantedMode, IBinder resultTo,
            String resultWho, int requestCode, boolean onlyIfNeeded,
            boolean debug, WaitResult outResult, Configuration config) {

        ......

        boolean componentSpecified = intent.getComponent() != null;

        // Don't modify the client's object!
        intent = new Intent(intent);

        // Collect information about the target of the Intent.
        ActivityInfo aInfo;
        try {
            ResolveInfo rInfo =
                AppGlobals.getPackageManager().resolveIntent(
                intent, resolvedType,
                PackageManager.MATCH_DEFAULT_ONLY
                | ActivityManagerService.STOCK_PM_FLAGS);
            aInfo = rInfo != null ? rInfo.activityInfo : null;
        } catch (RemoteException e) {
            ......
        }

        if (aInfo != null) {
            // Store the found target back into the intent, because now that
            // we have it we never want to do this again.  For example, if the
            // user navigates back to this point in the history, we should
            // always restart the exact same activity.
            intent.setComponent(new ComponentName(
                aInfo.applicationInfo.packageName, aInfo.name));
            ......
        }

        synchronized (mService) {
            int callingPid;
            int callingUid;
            if (caller == null) {
                ......
            } else {
                callingPid = callingUid = -1;
            }

            mConfigWillChange = config != null
                && mService.mConfiguration.diff(config) != 0;

            ......

            if (mMainStack && aInfo != null &&
                (aInfo.applicationInfo.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
                  
                      ......

            }

            int res = startActivityLocked(caller, intent, resolvedType,
                grantedUriPermissions, grantedMode, aInfo,
                resultTo, resultWho, requestCode, callingPid, callingUid,
                onlyIfNeeded, componentSpecified);

            if (mConfigWillChange && mMainStack) {
                ......
            }

            ......

            if (outResult != null) {
                ......
            }

            return res;
        }

    }

    ......

}
View Code

  注意,從Step 6傳下來的參數outResult和config均爲null

  此外,表達式(aInfo.applicationInfo.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0爲false

  下面語句對參數intent的內容進行解析,獲得MainActivity的相關信息,保存在aInfo變量中:

    ActivityInfo aInfo;
    try {
    ResolveInfo rInfo =
    AppGlobals.getPackageManager().resolveIntent(
        intent, resolvedType,
        PackageManager.MATCH_DEFAULT_ONLY
        | ActivityManagerService.STOCK_PM_FLAGS);
    aInfo = rInfo != null ? rInfo.activityInfo : null;
    } catch (RemoteException e) {
        ......
    }
View Code

  解析以後,獲得的aInfo.applicationInfo.packageName和aInfo.name即爲配置文件AndroidManifest.xml裏面配置的參數。

  此外,函數開始的地方調用intent.getComponent()函數的返回值不爲null,所以,這裏的componentSpecified變量爲true。

  接下去就調用startActivityLocked進一步處理了。

Step 8. ActivityStack.startActivityLocked

  這個函數定義在frameworks/base/services/java/com/android/server/am/ActivityStack.java文件中:
public class ActivityStack {

    ......

    final int startActivityLocked(IApplicationThread caller,
            Intent intent, String resolvedType,
            Uri[] grantedUriPermissions,
            int grantedMode, ActivityInfo aInfo, IBinder resultTo,
                String resultWho, int requestCode,
            int callingPid, int callingUid, boolean onlyIfNeeded,
            boolean componentSpecified) {
            int err = START_SUCCESS;

        ProcessRecord callerApp = null;
        if (caller != null) {
            callerApp = mService.getRecordForAppLocked(caller);
            if (callerApp != null) {
                callingPid = callerApp.pid;
                callingUid = callerApp.info.uid;
            } else {
                ......
            }
        }

        ......

        ActivityRecord sourceRecord = null;
        ActivityRecord resultRecord = null;
        if (resultTo != null) {
            int index = indexOfTokenLocked(resultTo);
            
            ......
                
            if (index >= 0) {
                sourceRecord = (ActivityRecord)mHistory.get(index);
                if (requestCode >= 0 && !sourceRecord.finishing) {
                    ......
                }
            }
        }

        int launchFlags = intent.getFlags();

        if ((launchFlags&Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0
            && sourceRecord != null) {
            ......
        }

        if (err == START_SUCCESS && intent.getComponent() == null) {
            ......
        }

        if (err == START_SUCCESS && aInfo == null) {
            ......
        }

        if (err != START_SUCCESS) {
            ......
        }

        ......

        ActivityRecord r = new ActivityRecord(mService, this, callerApp, callingUid,
            intent, resolvedType, aInfo, mService.mConfiguration,
            resultRecord, resultWho, requestCode, componentSpecified);

        ......

        return startActivityUncheckedLocked(r, sourceRecord,
            grantedUriPermissions, grantedMode, onlyIfNeeded, true);
    }


    ......

}
View Code

  從傳進來的參數caller獲得調用者的進程信息,並保存在callerApp變量中,這裏就是Launcher應用程序的進程信息了。

  前面說過,參數resultTo是Launcher這個Activity裏面的一個Binder對象,經過它能夠得到Launcher這個Activity的相關信息,保存在sourceRecord變量中。

  再接下來,建立即將要啓動的Activity的相關信息,並保存在r變量中:

ActivityRecord r = new ActivityRecord(mService, this, callerApp, callingUid,
    intent, resolvedType, aInfo, mService.mConfiguration,
    resultRecord, resultWho, requestCode, componentSpecified);
View Code

  接着調用startActivityUncheckedLocked函數進行下一步操做。

Step 9. ActivityStack.startActivityUncheckedLocked

  這個函數定義在frameworks/base/services/java/com/android/server/am/ActivityStack.java文件中:
public class ActivityStack {

    ......

    final int startActivityUncheckedLocked(ActivityRecord r,
        ActivityRecord sourceRecord, Uri[] grantedUriPermissions,
        int grantedMode, boolean onlyIfNeeded, boolean doResume) {
        final Intent intent = r.intent;
        final int callingUid = r.launchedFromUid;

        int launchFlags = intent.getFlags();

        // We'll invoke onUserLeaving before onPause only if the launching
        // activity did not explicitly state that this is an automated launch.
        mUserLeaving = (launchFlags&Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
        
        ......

        ActivityRecord notTop = (launchFlags&Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP)
            != 0 ? r : null;

        // If the onlyIfNeeded flag is set, then we can do this if the activity
        // being launched is the same as the one making the call...  or, as
        // a special case, if we do not know the caller then we count the
        // current top activity as the caller.
        if (onlyIfNeeded) {
            ......
        }

        if (sourceRecord == null) {
            ......
        } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
            ......
        } else if (r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE
            || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK) {
            ......
        }

        if (r.resultTo != null && (launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
            ......
        }

        boolean addingToTask = false;
        if (((launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
            (launchFlags&Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
            || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK
            || r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
                // If bring to front is requested, and no result is requested, and
                // we can find a task that was started with this same
                // component, then instead of launching bring that one to the front.
                if (r.resultTo == null) {
                    // See if there is a task to bring to the front.  If this is
                    // a SINGLE_INSTANCE activity, there can be one and only one
                    // instance of it in the history, and it is always in its own
                    // unique task, so we do a special search.
                    ActivityRecord taskTop = r.launchMode != ActivityInfo.LAUNCH_SINGLE_INSTANCE
                        ? findTaskLocked(intent, r.info)
                        : findActivityLocked(intent, r.info);
                    if (taskTop != null) {
                        ......
                    }
                }
        }

        ......

        if (r.packageName != null) {
            // If the activity being launched is the same as the one currently
            // at the top, then we need to check if it should only be launched
            // once.
            ActivityRecord top = topRunningNonDelayedActivityLocked(notTop);
            if (top != null && r.resultTo == null) {
                if (top.realActivity.equals(r.realActivity)) {
                    ......
                }
            }

        } else {
            ......
        }

        boolean newTask = false;

        // Should this be considered a new task?
        if (r.resultTo == null && !addingToTask
            && (launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
                // todo: should do better management of integers.
                mService.mCurTask++;
                if (mService.mCurTask <= 0) {
                    mService.mCurTask = 1;
                }
                r.task = new TaskRecord(mService.mCurTask, r.info, intent,
                    (r.info.flags&ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0);
                ......
                newTask = true;
                if (mMainStack) {
                    mService.addRecentTaskLocked(r.task);
                }

        } else if (sourceRecord != null) {
            ......
        } else {
            ......
        }

        ......

        startActivityLocked(r, newTask, doResume);
        return START_SUCCESS;
    }

    ......

}
View Code

  函數首先得到intent的標誌值,保存在launchFlags變量中。

  這個intent的標誌值的位Intent.FLAG_ACTIVITY_NO_USER_ACTION沒有置位,所以 ,成員變量mUserLeaving的值爲true。

  這個intent的標誌值的位Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP也沒有置位,所以,變量notTop的值爲null。

  因爲例子的AndroidManifest.xml文件中,MainActivity沒有配置launchMode屬值,所以,這裏的r.launchMode爲默認值0,表示以標準(Standard,或者稱爲ActivityInfo.LAUNCH_MULTIPLE)的方式來啓動這個Activity。
  Activity的啓動方式有四種,其他三種分別是ActivityInfo.LAUNCH_SINGLE_INSTANCE、ActivityInfo.LAUNCH_SINGLE_TASK和ActivityInfo.LAUNCH_SINGLE_TOP

傳進來的參數r.resultTo爲null,表示Launcher不須要等這個即將要啓動的MainActivity的執行結果。

  因爲這個intent的標誌值的位Intent.FLAG_ACTIVITY_NEW_TASK被置位,並且Intent.FLAG_ACTIVITY_MULTIPLE_TASK沒有置位,所以,下面的if語句會被執行:

    if (((launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
    (launchFlags&Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
    || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK
    || r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
        // If bring to front is requested, and no result is requested, and
        // we can find a task that was started with this same
        // component, then instead of launching bring that one to the front.
        if (r.resultTo == null) {
            // See if there is a task to bring to the front.  If this is
            // a SINGLE_INSTANCE activity, there can be one and only one
            // instance of it in the history, and it is always in its own
            // unique task, so we do a special search.
            ActivityRecord taskTop = r.launchMode != ActivityInfo.LAUNCH_SINGLE_INSTANCE
                ? findTaskLocked(intent, r.info)
                : findActivityLocked(intent, r.info);
            if (taskTop != null) {
                ......
            }
        }
    }
View Code

  這段代碼的邏輯是:

  查看一下,當前有沒有Task能夠用來執行這個Activity。因爲r.launchMode的值不爲ActivityInfo.LAUNCH_SINGLE_INSTANCE,所以,它經過findTaskLocked函數來查找存不存這樣的Task,這裏返回的結果是null,即taskTop爲null,所以,須要建立一個新的Task來啓動這個Activity。

  接着往下看:

    if (r.packageName != null) {
    // If the activity being launched is the same as the one currently
    // at the top, then we need to check if it should only be launched
    // once.
    ActivityRecord top = topRunningNonDelayedActivityLocked(notTop);
    if (top != null && r.resultTo == null) {
        if (top.realActivity.equals(r.realActivity)) {
            ......
        }
    }

    } 
View Code

  這段代碼的邏輯是:

  看一下當前在堆棧頂端的Activity是否就是即將要啓動的Activity,有些狀況下,若是即將要啓動的Activity就在堆棧的頂端,那麼,就不會從新啓動這個Activity的別一個實例了。

  如今處理堆棧頂端的Activity是Launcher,與咱們即將要啓動的MainActivity不是同一個Activity,所以,這裏不用進一步處理上述介紹的狀況。

  執行到這裏,咱們知道,要在一個新的Task裏面來啓動這個Activity了,因而新建立一個Task:

   if (r.resultTo == null && !addingToTask
    && (launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
    // todo: should do better management of integers.
    mService.mCurTask++;
    if (mService.mCurTask <= 0) {
        mService.mCurTask = 1;
    }
    r.task = new TaskRecord(mService.mCurTask, r.info, intent,
        (r.info.flags&ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0);
    ......
    newTask = true;
    if (mMainStack) {
        mService.addRecentTaskLocked(r.task);
    }

    }
View Code

  新建的Task保存在r.task域中,同時,添加到mService中去,這裏的mService就是ActivityManagerService了。

  最後就進入startActivityLocked(r, newTask, doResume)進一步處理了。這個函數定義在frameworks/base/services/java/com/android/server/am/ActivityStack.java文件中:

public class ActivityStack {

    ......

    private final void startActivityLocked(ActivityRecord r, boolean newTask,
            boolean doResume) {
        final int NH = mHistory.size();

        int addPos = -1;

        if (!newTask) {
            ......
        }

        // Place a new activity at top of stack, so it is next to interact
        // with the user.
        if (addPos < 0) {
            addPos = NH;
        }

        // If we are not placing the new activity frontmost, we do not want
        // to deliver the onUserLeaving callback to the actual frontmost
        // activity
        if (addPos < NH) {
            ......
        }

        // Slot the activity into the history stack and proceed
        mHistory.add(addPos, r);
        r.inHistory = true;
        r.frontOfTask = newTask;
        r.task.numActivities++;
        if (NH > 0) {
            // We want to show the starting preview window if we are
            // switching to a new task, or the next activity's process is
            // not currently running.
            ......
        } else {
            // If this is the first activity, don't do any fancy animations,
            // because there is nothing for it to animate on top of.
            ......
        }
        
        ......

        if (doResume) {
            resumeTopActivityLocked(null);
        }
    }

    ......

}
View Code
  這裏的NH表示當前系統中歷史任務的個數,這裏確定是大於0,由於Launcher已經跑起來了。當NH>0時,而且如今要切換新任務時,要作一些任務切換的界面操做,這裏不會影響到下面啓Activity的過程,有興趣的童鞋能夠本身研究一下。

  這裏傳進來的參數doResume爲true,因而調用resumeTopActivityLocked進一步操做。  

Step 10. Activity.resumeTopActivityLocked

  這個函數定義在frameworks/base/services/java/com/android/server/am/ActivityStack.java文件中:
public class ActivityStack {

    ......

    /**
    * Ensure that the top activity in the stack is resumed.
    *
    * @param prev The previously resumed activity, for when in the process
    * of pausing; can be null to call from elsewhere.
    *
    * @return Returns true if something is being resumed, or false if
    * nothing happened.
    */
    final boolean resumeTopActivityLocked(ActivityRecord prev) {
        // Find the first activity that is not finishing.
        ActivityRecord next = topRunningActivityLocked(null);

        // Remember how we'll process this pause/resume situation, and ensure
        // that the state is reset however we wind up proceeding.
        final boolean userLeaving = mUserLeaving;
        mUserLeaving = false;

        if (next == null) {
            ......
        }

        next.delayedResume = false;

        // If the top activity is the resumed one, nothing to do.
        if (mResumedActivity == next && next.state == ActivityState.RESUMED) {
            ......
        }

        // If we are sleeping, and there is no resumed activity, and the top
        // activity is paused, well that is the state we want.
        if ((mService.mSleeping || mService.mShuttingDown)
            && mLastPausedActivity == next && next.state == ActivityState.PAUSED) {
            ......
        }

        ......

        // If we are currently pausing an activity, then don't do anything
        // until that is done.
        if (mPausingActivity != null) {
            ......
        }

        ......

        // We need to start pausing the current activity so the top one
        // can be resumed...
        if (mResumedActivity != null) {
            ......
            startPausingLocked(userLeaving, false);
            return true;
        }

        ......
    }

    ......

}
View Code

  函數先經過調用topRunningActivityLocked函數得到堆棧頂端的Activity,這裏就是MainActivity了,這是在上面的Step 9設置好的,保存在next變量中。 

  接下來把mUserLeaving的保存在本地變量userLeaving中,而後從新設置爲false,在上面的Step 9中,mUserLeaving的值爲true,所以,這裏的userLeaving爲true。

  這裏的mResumedActivity爲Launcher,由於Launcher是當前正被執行的Activity。

  當咱們處理休眠狀態時,mLastPausedActivity保存堆棧頂端的Activity,由於當前不是休眠狀態,因此mLastPausedActivity爲null。

  有了這些信息以後,下面的語句就容易理解了:

    // If the top activity is the resumed one, nothing to do.
    if (mResumedActivity == next && next.state == ActivityState.RESUMED) {
    ......
    }

    // If we are sleeping, and there is no resumed activity, and the top
    // activity is paused, well that is the state we want.
    if ((mService.mSleeping || mService.mShuttingDown)
    && mLastPausedActivity == next && next.state == ActivityState.PAUSED) {
    ......
    }
View Code

  它首先看要啓動的Activity是否就是當前處理Resumed狀態的Activity,若是是的話,那就什麼都不用作,直接返回就能夠了;不然再看一下系統當前是否休眠狀態,若是是的話,再看看要啓動的Activity是否就是當前處於堆棧頂端的Activity,若是是的話,也是什麼都不用作。

  上面兩個條件都不知足,所以,在繼續往下執行以前,首先要把當處於Resumed狀態的Activity推入Paused狀態,而後才能夠啓動新的Activity。可是在將當前這個Resumed狀態的Activity推入Paused狀態以前,首先要看一下當前是否有Activity正在進入Pausing狀態,若是有的話,當前這個Resumed狀態的Activity就要稍後才能進入Paused狀態了,這樣就保證了全部須要進入Paused狀態的Activity串行處理。

  這裏沒有處於Pausing狀態的Activity,即mPausingActivity爲null,並且mResumedActivity也不爲null,因而就調用startPausingLocked函數把Launcher推入Paused狀態去了。

Step 11. ActivityStack.startPausingLocked

  這個函數定義在frameworks/base/services/java/com/android/server/am/ActivityStack.java文件中:
public class ActivityStack {

    ......

    private final void startPausingLocked(boolean userLeaving, boolean uiSleeping) {
        if (mPausingActivity != null) {
            ......
        }
        ActivityRecord prev = mResumedActivity;
        if (prev == null) {
            ......
        }
        ......
        mResumedActivity = null;
        mPausingActivity = prev;
        mLastPausedActivity = prev;
        prev.state = ActivityState.PAUSING;
        ......

        if (prev.app != null && prev.app.thread != null) {
            ......
            try {
                ......
                prev.app.thread.schedulePauseActivity(prev, prev.finishing, userLeaving,
                    prev.configChangeFlags);
                ......
            } catch (Exception e) {
                ......
            }
        } else {
            ......
        }

        ......
    
    }

    ......

}
View Code

  函數首先把mResumedActivity保存在本地變量prev中。在上一步Step 10中,說到mResumedActivity就是Launcher,所以,這裏把Launcher進程中的ApplicationThread對象取出來,經過它來通知Launcher這個Activity它要進入Paused狀態了。固然,這裏的prev.app.thread是一個ApplicationThread對象的遠程接口,經過調用這個遠程接口的schedulePauseActivity來通知Launcher進入Paused狀態。

  參數prev.finishing表示prev所表明的Activity是否正在等待結束的Activity列表中,因爲Laucher這個Activity還沒結束,因此這裏爲false;參數prev.configChangeFlags表示哪些config發生了變化,這裏咱們不關心它的值。

Step 12. ApplicationThreadProxy.schedulePauseActivity

  這個函數定義在frameworks/base/core/java/android/app/ApplicationThreadNative.java文件中:
class ApplicationThreadProxy implements IApplicationThread {
    
    ......

    public final void schedulePauseActivity(IBinder token, boolean finished,
    boolean userLeaving, int configChanges) throws RemoteException {
        Parcel data = Parcel.obtain();
        data.writeInterfaceToken(IApplicationThread.descriptor);
        data.writeStrongBinder(token);
        data.writeInt(finished ? 1 : 0);
        data.writeInt(userLeaving ? 1 :0);
        data.writeInt(configChanges);
        mRemote.transact(SCHEDULE_PAUSE_ACTIVITY_TRANSACTION, data, null,
            IBinder.FLAG_ONEWAY);
        data.recycle();
    }

    ......

}
View Code

  這個函數經過Binder進程間通訊機制進入到ApplicationThread.schedulePauseActivity函數中。

Step 13. ApplicationThread.schedulePauseActivity

  這個函數定義在frameworks/base/core/java/android/app/ActivityThread.java文件中,它是ActivityThread的內部類:
public final class ActivityThread {
    
    ......

    private final class ApplicationThread extends ApplicationThreadNative {
        
        ......

        public final void schedulePauseActivity(IBinder token, boolean finished,
                boolean userLeaving, int configChanges) {
            queueOrSendMessage(
                finished ? H.PAUSE_ACTIVITY_FINISHING : H.PAUSE_ACTIVITY,
                token,
                (userLeaving ? 1 : 0),
                configChanges);
        }

        ......

    }

    ......

}
View Code

  這裏調用的函數queueOrSendMessage是ActivityThread類的成員函數。

  上面說到,這裏的finished值爲false,所以,queueOrSendMessage的第一個參數值爲H.PAUSE_ACTIVITY,表示要暫停token所表明的Activity,即Launcher。

Step 14. ActivityThread.queueOrSendMessage

  這個函數定義在frameworks/base/core/java/android/app/ActivityThread.java文件中:

public final class ActivityThread {
    
    ......

    private final void queueOrSendMessage(int what, Object obj, int arg1) {
        queueOrSendMessage(what, obj, arg1, 0);
    }

    private final void queueOrSendMessage(int what, Object obj, int arg1, int arg2) {
        synchronized (this) {
            ......
            Message msg = Message.obtain();
            msg.what = what;
            msg.obj = obj;
            msg.arg1 = arg1;
            msg.arg2 = arg2;
            mH.sendMessage(msg);
        }
    }

    ......

}
View Code

  這裏首先將相關信息組裝成一個msg,而後經過mH成員變量發送出去,mH的類型是H,繼承於Handler類,是ActivityThread的內部類,所以,這個消息最後由H.handleMessage來處理。

Step 15. H.handleMessage

  這個函數定義在frameworks/base/core/java/android/app/ActivityThread.java文件中:

public final class ActivityThread {
    
    ......

    private final class H extends Handler {

        ......

        public void handleMessage(Message msg) {
            ......
            switch (msg.what) {
            
            ......
            
            case PAUSE_ACTIVITY:
                handlePauseActivity((IBinder)msg.obj, false, msg.arg1 != 0, msg.arg2);
                maybeSnapshot();
                break;

            ......

            }
        ......

    }

    ......

}
View Code

  這裏調用ActivityThread.handlePauseActivity進一步操做,msg.obj是一個ActivityRecord對象的引用,它表明的是Launcher這個Activity。

Step 16. ActivityThread.handlePauseActivity

  這個函數定義在frameworks/base/core/java/android/app/ActivityThread.java文件中:

public final class ActivityThread {
    
    ......

    private final void handlePauseActivity(IBinder token, boolean finished,
            boolean userLeaving, int configChanges) {

        ActivityClientRecord r = mActivities.get(token);
        if (r != null) {
            //Slog.v(TAG, "userLeaving=" + userLeaving + " handling pause of " + r);
            if (userLeaving) {
                performUserLeavingActivity(r);
            }

            r.activity.mConfigChangeFlags |= configChanges;
            Bundle state = performPauseActivity(token, finished, true);

            // Make sure any pending writes are now committed.
            QueuedWork.waitToFinish();

            // Tell the activity manager we have paused.
            try {
                ActivityManagerNative.getDefault().activityPaused(token, state);
            } catch (RemoteException ex) {
            }
        }
    }

    ......

}
View Code

  函數首先將Binder引用token轉換成ActivityRecord的遠程接口ActivityClientRecord,而後作了三個事情

  1. 若是userLeaving爲true,則經過調用performUserLeavingActivity函數來調用Activity.onUserLeaveHint通知Activity,用戶要離開它了;
  2. 調用performPauseActivity函數來調用Activity.onPause函數,咱們知道,在Activity的生命週期中,當它要讓位於其它的Activity時,系統就會調用它的onPause函數;
  3. 它通知ActivityManagerService,這個Activity已經進入Paused狀態了,ActivityManagerService如今能夠完成未竟的事情,即啓動MainActivity了。

Step 17. ActivityManagerProxy.activityPaused

  這個函數定義在frameworks/base/core/java/android/app/ActivityManagerNative.java文件中:

class ActivityManagerProxy implements IActivityManager
{
    ......

    public void activityPaused(IBinder token, Bundle state) throws RemoteException
    {
        Parcel data = Parcel.obtain();
        Parcel reply = Parcel.obtain();
        data.writeInterfaceToken(IActivityManager.descriptor);
        data.writeStrongBinder(token);
        data.writeBundle(state);
        mRemote.transact(ACTIVITY_PAUSED_TRANSACTION, data, reply, 0);
        reply.readException();
        data.recycle();
        reply.recycle();
    }

    ......

}
View Code

  這裏經過Binder進程間通訊機制就進入到ActivityManagerService.activityPaused函數中去了。

Step 18. ActivityManagerService.activityPaused

  這個函數定義在frameworks/base/services/java/com/android/server/am/ActivityManagerService.java文件中:

public final class ActivityManagerService extends ActivityManagerNative
            implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback {
    ......

    public final void activityPaused(IBinder token, Bundle icicle) {
        
        ......

        final long origId = Binder.clearCallingIdentity();
        mMainStack.activityPaused(token, icicle, false);
        
        ......
    }

    ......

}
View Code

  這裏,又再次進入到ActivityStack類中,執行activityPaused函數。

Step 19. ActivityStack.activityPaused

  這個函數定義在frameworks/base/services/java/com/android/server/am/ActivityStack.java文件中:

public class ActivityStack {

    ......

    final void activityPaused(IBinder token, Bundle icicle, boolean timeout) {
        
        ......

        ActivityRecord r = null;

        synchronized (mService) {
            int index = indexOfTokenLocked(token);
            if (index >= 0) {
                r = (ActivityRecord)mHistory.get(index);
                if (!timeout) {
                    r.icicle = icicle;
                    r.haveState = true;
                }
                mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
                if (mPausingActivity == r) {
                    r.state = ActivityState.PAUSED;
                    completePauseLocked();
                } else {
                    ......
                }
            }
        }
    }

    ......

}
View Code

  這裏經過參數token在mHistory列表中獲得ActivityRecord,從上面咱們知道,這個ActivityRecord表明的是Launcher這個Activity,而咱們在Step 11中,把Launcher這個Activity的信息保存在mPausingActivity中,所以,這裏mPausingActivity等於r,因而,執行completePauseLocked操做。

Step 20. ActivityStack.completePauseLocked

  這個函數定義在frameworks/base/services/java/com/android/server/am/ActivityStack.java文件中:

public class ActivityStack {

    ......

    private final void completePauseLocked() {
        ActivityRecord prev = mPausingActivity;
        
        ......

        if (prev != null) {

            ......

            mPausingActivity = null;
        }

        if (!mService.mSleeping && !mService.mShuttingDown) {
            resumeTopActivityLocked(prev);
        } else {
            ......
        }

        ......
    }

    ......

}
View Code

  函數首先把mPausingActivity變量清空,由於如今不須要它了,而後調用resumeTopActivityLokced進一步操做,它傳入的參數即爲表明Launcher這個Activity的ActivityRecord。

Step 21. ActivityStack.resumeTopActivityLokced

  這個函數定義在frameworks/base/services/java/com/android/server/am/ActivityStack.java文件中:

public class ActivityStack {

    ......

    final boolean resumeTopActivityLocked(ActivityRecord prev) {
        ......

        // Find the first activity that is not finishing.
        ActivityRecord next = topRunningActivityLocked(null);

        // Remember how we'll process this pause/resume situation, and ensure
        // that the state is reset however we wind up proceeding.
        final boolean userLeaving = mUserLeaving;
        mUserLeaving = false;

        ......

        next.delayedResume = false;

        // If the top activity is the resumed one, nothing to do.
        if (mResumedActivity == next && next.state == ActivityState.RESUMED) {
            ......
            return false;
        }

        // If we are sleeping, and there is no resumed activity, and the top
        // activity is paused, well that is the state we want.
        if ((mService.mSleeping || mService.mShuttingDown)
            && mLastPausedActivity == next && next.state == ActivityState.PAUSED) {
            ......
            return false;
        }

        .......


        // We need to start pausing the current activity so the top one
        // can be resumed...
        if (mResumedActivity != null) {
            ......
            return true;
        }

        ......


        if (next.app != null && next.app.thread != null) {
            ......

        } else {
            ......
            startSpecificActivityLocked(next, true, true);
        }

        return true;
    }


    ......

}
View Code

  經過上面的Step 9,咱們知道,當前在堆棧頂端的Activity爲咱們即將要啓動的MainActivity,這裏經過調用topRunningActivityLocked將它取回來,保存在next變量中。以前最後一個Resumed狀態的Activity,即Launcher,到了這裏已經處於Paused狀態了,所以,mResumedActivity爲null。最後一個處於Paused狀態的Activity爲Launcher,所以,這裏的mLastPausedActivity就爲Launcher。前面咱們爲MainActivity建立了ActivityRecord後,它的app域一直保持爲null。有了這些信息後,上面這段代碼就容易理解了,它最終調用startSpecificActivityLocked進行下一步操做。

Step 22. ActivityStack.startSpecificActivityLocked

  這個函數定義在frameworks/base/services/java/com/android/server/am/ActivityStack.java文件中:

public class ActivityStack {

    ......

    private final void startSpecificActivityLocked(ActivityRecord r,
            boolean andResume, boolean checkConfig) {
        // Is this activity's application already running?
        ProcessRecord app = mService.getProcessRecordLocked(r.processName,
            r.info.applicationInfo.uid);

        ......

        if (app != null && app.thread != null) {
            try {
                realStartActivityLocked(r, app, andResume, checkConfig);
                return;
            } catch (RemoteException e) {
                ......
            }
        }

        mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
            "activity", r.intent.getComponent(), false);
    }


    ......

}
View Code

  注意,這裏因爲是第一次啓動應用程序的Activity,因此下面語句:

ProcessRecord app = mService.getProcessRecordLocked(r.processName,  
    r.info.applicationInfo.uid);
View Code

  取回來的app爲null。

  在Activity應用程序中的AndroidManifest.xml配置文件中,咱們沒有指定Application標籤的process屬性,系統就會默認使用package的名稱。

  每個應用程序都有本身的uid,所以,這裏uid + process的組合就能夠爲每個應用程序建立一個ProcessRecord。固然,咱們能夠配置兩個應用程序具備相同的uid和package,或者在AndroidManifest.xml配置文件的application標籤或者activity標籤中顯式指定相同的process屬性值,這樣,不一樣的應用程序也能夠在同一個進程中啓動。

  函數最終執行ActivityManagerService.startProcessLocked函數進行下一步操做。

Step 23. ActivityManagerService.startProcessLocked

  這個函數定義在frameworks/base/services/java/com/android/server/am/ActivityManagerService.java文件中:

public final class ActivityManagerService extends ActivityManagerNative
        implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback {

    ......

    final ProcessRecord startProcessLocked(String processName,
            ApplicationInfo info, boolean knownToBeDead, int intentFlags,
            String hostingType, ComponentName hostingName, boolean allowWhileBooting) {

        ProcessRecord app = getProcessRecordLocked(processName, info.uid);
        
        ......

        String hostingNameStr = hostingName != null
            ? hostingName.flattenToShortString() : null;

        ......

        if (app == null) {
            app = new ProcessRecordLocked(null, info, processName);
            mProcessNames.put(processName, info.uid, app);
        } else {
            // If this is a new package in the process, add the package to the list
            app.addPackage(info.packageName);
        }

        ......

        startProcessLocked(app, hostingType, hostingNameStr);
        return (app.pid != 0) ? app : null;
    }

    ......

}
View Code

  這裏再次檢查是否已經有以process + uid命名的進程存在,在咱們這個情景中,返回值app爲null,所以,後面會建立一個ProcessRecord,並保存在成員變量mProcessNames中,最後,調用另外一個startProcessLocked函數進一步操做:

public final class ActivityManagerService extends ActivityManagerNative
        implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback {

    ......

    private final void startProcessLocked(ProcessRecord app,
                String hostingType, String hostingNameStr) {

        ......

        try {
            int uid = app.info.uid;
            int[] gids = null;
            try {
                gids = mContext.getPackageManager().getPackageGids(
                    app.info.packageName);
            } catch (PackageManager.NameNotFoundException e) {
                ......
            }
            
            ......

            int debugFlags = 0;
            
            ......
            
            int pid = Process.start("android.app.ActivityThread",
                mSimpleProcessManagement ? app.processName : null, uid, uid,
                gids, debugFlags, null);
            
            ......

        } catch (RuntimeException e) {
            
            ......

        }
    }

    ......

}
View Code

  這裏主要是調用Process.start接口來建立一個新的進程,新的進程會導入android.app.ActivityThread類,而且執行它的main函數,這就是爲何咱們前面說每個應用程序都有一個ActivityThread實例來對應的緣由。

Step 24. ActivityThread.main

  這個函數定義在frameworks/base/core/java/android/app/ActivityThread.java文件中:

public final class ActivityThread {

    ......

    private final void attach(boolean system) {
        ......

        mSystemThread = system;
        if (!system) {

            ......

            IActivityManager mgr = ActivityManagerNative.getDefault();
            try {
                mgr.attachApplication(mAppThread);
            } catch (RemoteException ex) {
            }
        } else {

            ......

        }
    }

    ......

    public static final void main(String[] args) {
        
        .......

        ActivityThread thread = new ActivityThread();
        thread.attach(false);

        ......

        Looper.loop();

        .......

        thread.detach();
        
        ......
    }
}
View Code
  這個函數在進程中建立一個ActivityThread實例,而後調用它的attach函數,接着就進入消息循環了,直到最後進程退出。

  函數attach最終調用了ActivityManagerService的遠程接口ActivityManagerProxy的attachApplication函數,傳入的參數是mAppThread,這是一個ApplicationThread類型的Binder對象,它的做用是用來進行進程間通訊的。

Step 25. ActivityManagerProxy.attachApplication

  這個函數定義在frameworks/base/core/java/android/app/ActivityManagerNative.java文件中:

class ActivityManagerProxy implements IActivityManager
{
    ......

    public void attachApplication(IApplicationThread app) throws RemoteException
    {
        Parcel data = Parcel.obtain();
        Parcel reply = Parcel.obtain();
        data.writeInterfaceToken(IActivityManager.descriptor);
        data.writeStrongBinder(app.asBinder());
        mRemote.transact(ATTACH_APPLICATION_TRANSACTION, data, reply, 0);
        reply.readException();
        data.recycle();
        reply.recycle();
    }

    ......

}
View Code

  這裏經過Binder驅動程序,最後進入ActivityManagerService的attachApplication函數中。

Step 26. ActivityManagerService.attachApplication

  這個函數定義在frameworks/base/services/java/com/android/server/am/ActivityManagerService.java文件中:

public final class ActivityManagerService extends ActivityManagerNative
        implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback {

    ......

    public final void attachApplication(IApplicationThread thread) {
        synchronized (this) {
            int callingPid = Binder.getCallingPid();
            final long origId = Binder.clearCallingIdentity();
            attachApplicationLocked(thread, callingPid);
            Binder.restoreCallingIdentity(origId);
        }
    }

    ......

}
View Code

  這裏將操做轉發給attachApplicationLocked函數。

Step 27. ActivityManagerService.attachApplicationLocked

  這個函數定義在frameworks/base/services/java/com/android/server/am/ActivityManagerService.java文件中:

public final class ActivityManagerService extends ActivityManagerNative
        implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback {

    ......

    private final boolean attachApplicationLocked(IApplicationThread thread,
            int pid) {
        // Find the application record that is being attached...  either via
        // the pid if we are running in multiple processes, or just pull the
        // next app record if we are emulating process with anonymous threads.
        ProcessRecord app;
        if (pid != MY_PID && pid >= 0) {
            synchronized (mPidsSelfLocked) {
                app = mPidsSelfLocked.get(pid);
            }
        } else if (mStartingProcesses.size() > 0) {
            ......
        } else {
            ......
        }

        if (app == null) {
            ......
            return false;
        }

        ......

        String processName = app.processName;
        try {
            thread.asBinder().linkToDeath(new AppDeathRecipient(
                app, pid, thread), 0);
        } catch (RemoteException e) {
            ......
            return false;
        }

        ......

        app.thread = thread;
        app.curAdj = app.setAdj = -100;
        app.curSchedGroup = Process.THREAD_GROUP_DEFAULT;
        app.setSchedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE;
        app.forcingToForeground = null;
        app.foregroundServices = false;
        app.debugging = false;

        ......

        boolean normalMode = mProcessesReady || isAllowedWhileBooting(app.info);

        ......

        boolean badApp = false;
        boolean didSomething = false;

        // See if the top visible activity is waiting to run in this process...
        ActivityRecord hr = mMainStack.topRunningActivityLocked(null);
        if (hr != null && normalMode) {
            if (hr.app == null && app.info.uid == hr.info.applicationInfo.uid
                && processName.equals(hr.processName)) {
                    try {
                        if (mMainStack.realStartActivityLocked(hr, app, true, true)) {
                            didSomething = true;
                        }
                    } catch (Exception e) {
                        ......
                    }
            } else {
                ......
            }
        }

        ......

        return true;
    }

    ......

}
View Code

  在前面的Step 23中,已經建立了一個ProcessRecord,這裏首先經過pid將它取回來,放在app變量中,而後對app的其它成員進行初始化,最後調用mMainStack.realStartActivityLocked執行真正的Activity啓動操做。這裏要啓動的Activity經過調用mMainStack.topRunningActivityLocked(null)從堆棧頂端取回來,這時候在堆棧頂端的Activity就是MainActivity了。

Step 28. ActivityStack.realStartActivityLocked

  這個函數定義在frameworks/base/services/java/com/android/server/am/ActivityStack.java文件中:

public class ActivityStack {

    ......

    final boolean realStartActivityLocked(ActivityRecord r,
            ProcessRecord app, boolean andResume, boolean checkConfig)
            throws RemoteException {
        
        ......

        r.app = app;

        ......

        int idx = app.activities.indexOf(r);
        if (idx < 0) {
            app.activities.add(r);
        }
        
        ......

        try {
            ......

            List<ResultInfo> results = null;
            List<Intent> newIntents = null;
            if (andResume) {
                results = r.results;
                newIntents = r.newIntents;
            }
    
            ......
            
            app.thread.scheduleLaunchActivity(new Intent(r.intent), r,
                System.identityHashCode(r),
                r.info, r.icicle, results, newIntents, !andResume,
                mService.isNextTransitionForward());

            ......

        } catch (RemoteException e) {
            ......
        }

        ......

        return true;
    }

    ......

}
View Code

  這裏最終經過app.thread進入到ApplicationThreadProxy的scheduleLaunchActivity函數中,注意,這裏的第二個參數r,是一個ActivityRecord類型的Binder對象,用來做爲這個Activity的token值。

Step 29. ApplicationThreadProxy.scheduleLaunchActivity

  這個函數定義在frameworks/base/core/java/android/app/ApplicationThreadNative.java文件中:

class ApplicationThreadProxy implements IApplicationThread {

    ......

    public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
            ActivityInfo info, Bundle state, List<ResultInfo> pendingResults,
            List<Intent> pendingNewIntents, boolean notResumed, boolean isForward)
            throws RemoteException {
        Parcel data = Parcel.obtain();
        data.writeInterfaceToken(IApplicationThread.descriptor);
        intent.writeToParcel(data, 0);
        data.writeStrongBinder(token);
        data.writeInt(ident);
        info.writeToParcel(data, 0);
        data.writeBundle(state);
        data.writeTypedList(pendingResults);
        data.writeTypedList(pendingNewIntents);
        data.writeInt(notResumed ? 1 : 0);
        data.writeInt(isForward ? 1 : 0);
        mRemote.transact(SCHEDULE_LAUNCH_ACTIVITY_TRANSACTION, data, null,
            IBinder.FLAG_ONEWAY);
        data.recycle();
    }

    ......

}
View Code

  這個函數最終經過Binder驅動程序進入到ApplicationThread的scheduleLaunchActivity函數中。

Step 30. ApplicationThread.scheduleLaunchActivity

  這個函數定義在frameworks/base/core/java/android/app/ActivityThread.java文件中:

public final class ActivityThread {

    ......

    private final class ApplicationThread extends ApplicationThreadNative {

        ......

        // we use token to identify this activity without having to send the
        // activity itself back to the activity manager. (matters more with ipc)
        public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
                ActivityInfo info, Bundle state, List<ResultInfo> pendingResults,
                List<Intent> pendingNewIntents, boolean notResumed, boolean isForward) {
            ActivityClientRecord r = new ActivityClientRecord();

            r.token = token;
            r.ident = ident;
            r.intent = intent;
            r.activityInfo = info;
            r.state = state;

            r.pendingResults = pendingResults;
            r.pendingIntents = pendingNewIntents;

            r.startsNotResumed = notResumed;
            r.isForward = isForward;

            queueOrSendMessage(H.LAUNCH_ACTIVITY, r);
        }

        ......

    }

    ......
}
View Code

  函數首先建立一個ActivityClientRecord實例,而且初始化它的成員變量,而後調用ActivityThread類的queueOrSendMessage函數進一步處理。

Step 31. ActivityThread.queueOrSendMessage

  這個函數定義在frameworks/base/core/java/android/app/ActivityThread.java文件中:

public final class ActivityThread {

    ......

    private final class ApplicationThread extends ApplicationThreadNative {

        ......

        // if the thread hasn't started yet, we don't have the handler, so just
        // save the messages until we're ready.
        private final void queueOrSendMessage(int what, Object obj) {
            queueOrSendMessage(what, obj, 0, 0);
        }

        ......

        private final void queueOrSendMessage(int what, Object obj, int arg1, int arg2) {
            synchronized (this) {
                ......
                Message msg = Message.obtain();
                msg.what = what;
                msg.obj = obj;
                msg.arg1 = arg1;
                msg.arg2 = arg2;
                mH.sendMessage(msg);
            }
        }

        ......

    }

    ......
}
View Code

  函數把消息內容放在msg中,而後經過mH把消息分發出去,這裏的成員變量mH咱們在前面已經見過,消息分發出去後,最後會調用H類的handleMessage函數。

Step 32. H.handleMessage

  這個函數定義在frameworks/base/core/java/android/app/ActivityThread.java文件中:

public final class ActivityThread {

    ......

    private final class H extends Handler {

        ......

        public void handleMessage(Message msg) {
            ......
            switch (msg.what) {
            case LAUNCH_ACTIVITY: {
                ActivityClientRecord r = (ActivityClientRecord)msg.obj;

                r.packageInfo = getPackageInfoNoCheck(
                    r.activityInfo.applicationInfo);
                handleLaunchActivity(r, null);
            } break;
            ......
            }

        ......

    }

    ......
}
View Code

  這裏最後調用ActivityThread類的handleLaunchActivity函數進一步處理。

Step 33. ActivityThread.handleLaunchActivity

  這個函數定義在frameworks/base/core/java/android/app/ActivityThread.java文件中:

public final class ActivityThread {

    ......

    private final void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) {
        ......

        Activity a = performLaunchActivity(r, customIntent);

        if (a != null) {
            r.createdConfig = new Configuration(mConfiguration);
            Bundle oldState = r.state;
            handleResumeActivity(r.token, false, r.isForward);

            ......
        } else {
            ......
        }
    }

    ......
}
View Code

  這裏首先調用performLaunchActivity函數來加載這個Activity類,而後調用它的onCreate函數,最後回到handleLaunchActivity函數時,再調用handleResumeActivity函數來使這個Activity進入Resumed狀態,即會調用這個Activity的onResume函數,這是遵循Activity的生命週期的。

Step 34. ActivityThread.performLaunchActivity

  這個函數定義在frameworks/base/core/java/android/app/ActivityThread.java文件中:

public final class ActivityThread {

    ......

    private final Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
        
        ActivityInfo aInfo = r.activityInfo;
        if (r.packageInfo == null) {
            r.packageInfo = getPackageInfo(aInfo.applicationInfo,
                Context.CONTEXT_INCLUDE_CODE);
        }

        ComponentName component = r.intent.getComponent();
        if (component == null) {
            component = r.intent.resolveActivity(
                mInitialApplication.getPackageManager());
            r.intent.setComponent(component);
        }

        if (r.activityInfo.targetActivity != null) {
            component = new ComponentName(r.activityInfo.packageName,
                r.activityInfo.targetActivity);
        }

        Activity activity = null;
        try {
            java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
            activity = mInstrumentation.newActivity(
                cl, component.getClassName(), r.intent);
            r.intent.setExtrasClassLoader(cl);
            if (r.state != null) {
                r.state.setClassLoader(cl);
            }
        } catch (Exception e) {
            ......
        }

        try {
            Application app = r.packageInfo.makeApplication(false, mInstrumentation);

            ......

            if (activity != null) {
                ContextImpl appContext = new ContextImpl();
                appContext.init(r.packageInfo, r.token, this);
                appContext.setOuterContext(activity);
                CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
                Configuration config = new Configuration(mConfiguration);
                ......
                activity.attach(appContext, this, getInstrumentation(), r.token,
                    r.ident, app, r.intent, r.activityInfo, title, r.parent,
                    r.embeddedID, r.lastNonConfigurationInstance,
                    r.lastNonConfigurationChildInstances, config);

                if (customIntent != null) {
                    activity.mIntent = customIntent;
                }
                r.lastNonConfigurationInstance = null;
                r.lastNonConfigurationChildInstances = null;
                activity.mStartedActivity = false;
                int theme = r.activityInfo.getThemeResource();
                if (theme != 0) {
                    activity.setTheme(theme);
                }

                activity.mCalled = false;
                mInstrumentation.callActivityOnCreate(activity, r.state);
                ......
                r.activity = activity;
                r.stopped = true;
                if (!r.activity.mFinished) {
                    activity.performStart();
                    r.stopped = false;
                }
                if (!r.activity.mFinished) {
                    if (r.state != null) {
                        mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state);
                    }
                }
                if (!r.activity.mFinished) {
                    activity.mCalled = false;
                    mInstrumentation.callActivityOnPostCreate(activity, r.state);
                    if (!activity.mCalled) {
                        throw new SuperNotCalledException(
                            "Activity " + r.intent.getComponent().toShortString() +
                            " did not call through to super.onPostCreate()");
                    }
                }
            }
            r.paused = true;

            mActivities.put(r.token, r);

        } catch (SuperNotCalledException e) {
            ......

        } catch (Exception e) {
            ......
        }

        return activity;
    }

    ......
}
View Code

  函數前面是收集要啓動的Activity的相關信息,主要package和component信息:

   ActivityInfo aInfo = r.activityInfo;
   if (r.packageInfo == null) {
        r.packageInfo = getPackageInfo(aInfo.applicationInfo,
                Context.CONTEXT_INCLUDE_CODE);
   }

   ComponentName component = r.intent.getComponent();
   if (component == null) {
       component = r.intent.resolveActivity(
           mInitialApplication.getPackageManager());
       r.intent.setComponent(component);
   }

   if (r.activityInfo.targetActivity != null) {
       component = new ComponentName(r.activityInfo.packageName,
               r.activityInfo.targetActivity);
   }
View Code

   而後經過ClassLoader將MainActivity類加載進來:

   Activity activity = null;
   try {
    java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
    activity = mInstrumentation.newActivity(
        cl, component.getClassName(), r.intent);
    r.intent.setExtrasClassLoader(cl);
    if (r.state != null) {
        r.state.setClassLoader(cl);
    }
   } catch (Exception e) {
    ......
   }
View Code

  接下來是建立Application對象,這是根據AndroidManifest.xml配置文件中的Application標籤的信息來建立的:

Application app = r.packageInfo.makeApplication(false, mInstrumentation);  
View Code

  後面的代碼主要建立Activity的上下文信息,並經過attach方法將這些上下文信息設置到MainActivity中去:

   activity.attach(appContext, this, getInstrumentation(), r.token,
    r.ident, app, r.intent, r.activityInfo, title, r.parent,
    r.embeddedID, r.lastNonConfigurationInstance,
    r.lastNonConfigurationChildInstances, config);
View Code

  最後還要調用MainActivity的onCreate函數:

mInstrumentation.callActivityOnCreate(activity, r.state);  
View Code

  這裏不是直接調用MainActivity的onCreate函數,而是經過mInstrumentation的callActivityOnCreate函數來間接調用,前面咱們說過,mInstrumentation在這裏的做用是監控Activity與系統的交互操做,至關因而系統運行日誌。

Step 35. MainActivity.onCreate

  這個函數就是咱們自定義的app工程文件。

  這樣,MainActivity就啓動起來了,整個應用程序也啓動起來了。


總結

  整個應用程序的啓動過程要執行不少步驟,可是總體來看,主要分爲如下五個階段:

  1. Step1 - Step 11:Launcher經過Binder進程間通訊機制通知ActivityManagerService,它要啓動一個Activity;
  2. Step 12 - Step 16:ActivityManagerService經過Binder進程間通訊機制通知Launcher進入Paused狀態;
  3. Step 17 - Step 24:Launcher經過Binder進程間通訊機制通知ActivityManagerService,它已經準備就緒進入Paused狀態,因而ActivityManagerService就建立一個新的進程,用來啓動一個ActivityThread實例,即將要啓動的Activity就是在這個ActivityThread實例中運行;
  4. Step 25 - Step 27:ActivityThread經過Binder進程間通訊機制將一個ApplicationThread類型的Binder對象傳遞給ActivityManagerService,以便之後ActivityManagerService可以經過這個Binder對象和它進行通訊;
  5. Step 28 - Step 35:ActivityManagerService經過Binder進程間通訊機制通知ActivityThread,如今一切準備就緒,它能夠真正執行Activity的啓動操做了。
  這樣,應用程序的啓動過程就介紹完了,它實質上是啓動應用程序的默認Activity
 
 

參考文章

  http://blog.csdn.net/luoshengyang/article/details/6689748
相關文章
相關標籤/搜索