此次從Activity的handleLaunchActivity(...)方法開始分析,由於前面的流程已經在建立Application過程當中講過了。 從代碼中咱們能夠看出Activity是經過performLauncherActivity(...)方法建立的。咱們看下這個方法幹了啥java
private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
...
ContextImpl appContext = createBaseContextForActivity(r);
Activity activity = null;
try {
java.lang.ClassLoader cl = appContext.getClassLoader();
activity = mInstrumentation.newActivity(
cl, component.getClassName(), r.intent);
StrictMode.incrementExpectedActivityCount(activity.getClass());
r.intent.setExtrasClassLoader(cl);
r.intent.prepareToEnterProcess();
if (r.state != null) {
r.state.setClassLoader(cl);
}
} catch (Exception e) {
if (!mInstrumentation.onException(activity, e)) {
throw new RuntimeException(
"Unable to instantiate activity " + component+ ": " + e.toString(), e);
}
}
...
if (r.isPersistable()) {
mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState);
} else {
mInstrumentation.callActivityOnCreate(activity, r.state);
}
if (!activity.mCalled) {
throw new SuperNotCalledException(
"Activity " + r.intent.getComponent().toShortString() +
" did not call through to super.onCreate()");
}
r.activity = activity;
r.stopped = true;
if (!r.activity.mFinished) {
activity.performStart();
r.stopped = false;
}
if (!r.activity.mFinished) {
if (r.isPersistable()) {
if (r.state != null || r.persistentState != null) {
mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state,
r.persistentState);
}
} else if (r.state != null) {
mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state);
}
}
...
return activity;
}
複製代碼
從代碼中咱們能夠看出:bash
- 首先建立了ContextImpl,而後經過Intrumentation的newActivity(...)來建立Activity的實例。
- 而後經過Intrumentation的callActivityOnCreate(...)來調用Activity的onCreate()方法
- 經過activity.performStart(...)來調用Activity的onStart()方法
- 若是上次Activity被異常殺死,則經過Intrumentation的callActivityOnRestoreInstanceState(...)來執行Activity的performRestoreInstanceState(...)方法
- 執行完成後則返回Activity實例並回到handleLaunchActivity(...)方法中,經過handleResumeActivity(...)方法來調用Activity的onResume方法。 以上就是Activity的建立流程.
AMS完成AMS進程中的Service建立後,經過ApplitionThread的scheduleCreateService(...)方法來經過ActivityThread來建立Client端的Service。下面咱們看下scheduleCreateService(...)的源碼app
public final void scheduleCreateService(IBinder token,
ServiceInfo info, CompatibilityInfo compatInfo, int processState) {
updateProcessState(processState, false);
CreateServiceData s = new CreateServiceData();
s.token = token;
s.info = info;
s.compatInfo = compatInfo;
sendMessage(H.CREATE_SERVICE, s);
}
複製代碼
scheduleCreateService(...)方法會發送CREATE_SERVICE給UI線程,下面看看UI線程的針對這個Message幹了什麼ui
public void handleMessage(Message msg) {
if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + codeToString(msg.what));
switch (msg.what) {
...
case CREATE_SERVICE:
Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, ("serviceCreate: " + String.valueOf(msg.obj)));
handleCreateService((CreateServiceData)msg.obj);
Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
break;
...
}
}
複製代碼
UI線程收到CREATE_SERVICE這個message後調用了handleCreateService(...)方法,接着看下handleCreateService(...)幹了什麼this
private void handleCreateService(CreateServiceData data) {
...
LoadedApk loadedApk = getLoadedApkNoCheck(
data.info.applicationInfo, data.compatInfo);
Service service = null;
try {
java.lang.ClassLoader cl = loadedApk.getClassLoader();
service = (Service) cl.loadClass(data.info.name).newInstance();
} catch (Exception e) {
if (!mInstrumentation.onException(service, e)) {
throw new RuntimeException(
"Unable to instantiate service " + data.info.name + ": " + e.toString(), e);
}
}
try {
if (localLOGV) Slog.v(TAG, "Creating service " + data.info.name);
ContextImpl context = ContextImpl.createAppContext(this, loadedApk);
context.setOuterContext(service);
Application app = loadedApk.makeApplication(false, mInstrumentation);
service.attach(context, this, data.info.name, data.token, app,
ActivityManager.getService());
service.onCreate();
mServices.put(data.token, service);
try {
ActivityManager.getService().serviceDoneExecuting(
data.token, SERVICE_DONE_EXECUTING_ANON, 0, 0);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
} catch (Exception e) {
if (!mInstrumentation.onException(service, e)) {
throw new RuntimeException(
"Unable to create service " + data.info.name + ": " + e.toString(), e);
}
}
}
複製代碼
從源碼中咱們能夠看出,首先經過ClassLoader來建立Service的實例。而後建立ContextImpl來讓Service attach,由此能夠證實,Service對應的mBase也爲ContextImpl,而後直接調用Service的onCreate()。spa