从main方法开始
与简单的控制台程序类似,Android应用也需要一个入口方法,但是这个入口方法并非是由我们自己定义的。首先比较控制台的“helloworld”与Android下的“helloworld”,前者很简单的一条线执行下去,而后者的Activity则存在着复杂的生命周期,从onCreate到onStart一系列方法之后还不会自行结束,很显然其实际执行过程不会像看上去那么简单。
要实现像Android应用的运行过程,最基本的需要一个循环来保证程序不会立即结束;如果要在运行的时候能够处理点击等事件,那么还需要在循环内加上消息的处理。于是很显然的,我们需要Handler。
ActivityThread
不管咋说一个入口是必须的,而入口则定义在ActivityThread内(给应用的Application的构造加个断点,调试,即可看到调用顺序。在ZygoteInit中的操作都是对运行环境的初始化,之后反射调用了ActivityThread.main())。可以很明显的看到主线程中存在消息循环,可以在H类中看到定义的一大堆消息。
public static void main(String []args)
{
//代码只贴出相关关键部分
Looper.prepareMainLooper();
ActivityThread thread = new ActivityThread();
thread.attach(false);
Looper.loop();
}
attach方法主要工作只是通过Binder间接调用ActivityManagerService.attachApplication(ActivityThread)。
private void attach(boolean system) {
if (!system) {
final IActivityManager mgr = ActivityManagerNative.getDefault();
try {
mgr.attachApplication(mAppThread);
} catch (RemoteException ex) {}
} else {
try {
mInstrumentation = new Instrumentation();
ContextImpl context = ContextImpl.createAppContext(
this, getSystemContext().mPackageInfo);
mInitialApplication = context.mPackageInfo.makeApplication(true, null);
mInitialApplication.onCreate();
} catch (Exception e) {
throw new RuntimeException(
"Unable to instantiate Application():" + e.toString(), e);
}
}
}
ActivityManagerService.attachApplication只是调用了attachApplicationLocked,attachApplicationLocked则通过binder调用ActivityThread.bindApplication,随后调用handleBindApplication:
try {
Application app = data.info.makeApplication(data.restrictedBackupMode, null); //构造应用程序的Application,并且调用它的attach方法
mInitialApplication = app;
try {
mInstrumentation.callApplicationOnCreate(app); //Application的onCreate调用
} catch (Exception e) {}
}
这里已经可以看到对应用程序的Application的初始化,之后继续在ActivityManagerService.attachApplicationLocked中调用ActivityStackSupervisor.attachApplicationLocked。顺水推舟:
ActivityThread.scheduleLaunchActivity()->handleLaunchActivity()。
其内:performLaunchActivity创建Activity并调用attach,onCreate,onStart;
handleResumeActivity->performResumeActivity调用onResume,即Activity的生命周期.(performResume就不贴了,结构差不多).
private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
Activity activity = null;
try {
java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
activity = mInstrumentation.newActivity(
cl, component.getClassName(), r.intent); //构造Activity
} catch (Exception e) { }
try {
Application app = r.packageInfo.makeApplication(false, mInstrumentation);//得到Application对象
if (activity != null) {
Context appContext = createBaseContextForActivity(r, activity);
activity.attach(appContext, this, getInstrumentation(), r.token,
r.ident, app, r.intent, r.activityInfo, title, r.parent,
r.embeddedID, r.lastNonConfigurationInstances, config,
r.referrer, r.voiceInteractor); //Activity.attach
if (customIntent != null) {
activity.mIntent = customIntent;
}
r.lastNonConfigurationInstances = null;
activity.mStartedActivity = false;
int theme = r.activityInfo.getThemeResource();
if (theme != 0) {
activity.setTheme(theme);
}
activity.mCalled = false;
if (r.isPersistable()) {
mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState);
} else {
mInstrumentation.callActivityOnCreate(activity, r.state);
} //Activity.onCreate
}
r.activity = activity;
r.stopped = true;
if (!r.activity.mFinished) {
activity.performStart(); //Activity.onStart
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);
} //Activity.performRestoreInstanceState
}
if (!r.activity.mFinished) {
if (r.isPersistable()) {
mInstrumentation.callActivityOnPostCreate(activity, r.state,
r.persistentState);
} else {
mInstrumentation.callActivityOnPostCreate(activity, r.state);
} //Activity.onPostCreate
}
}
} catch (Exception e) {}
}
}
应用程序启动完成,之后进入消息循环状态。
注:以上均为sdk-23的源码,其他版本会稍有不同。
参考:
http://www.jianshu.com/p/a1f40b39b3de
Android SDK部分源码