android app 启动过程,Android应用程序启动过程源代码分析(4)

Step 28. ActivityStack.realStartActivityLocked

这个函数定义在frameworks/base/services/java/com/android/server/am/ActivityStack.java文件中:

publicclassActivityStack {

......

finalbooleanrealStartActivityLocked(ActivityRecord r,

Proce***ecord app, booleanandResume,booleancheckConfig)

throwsRemoteException {

......

r.app = app;

......

intidx = app.activities.indexOf(r);

if(idx <0) {

app.activities.add(r);

}

......

try{

......

List results = null;

List newIntents = null;

if(andResume) {

results = r.results;

newIntents = r.newIntents;

}

......

app.thread.scheduleLaunchActivity(newIntent(r.intent), r,

System.identityHashCode(r),

r.info, r.icicle, results, newIntents, !andResume,

mService.isNextTransitionForward());

......

} catch(RemoteException e) {

......

}

......

returntrue;

}

......

}

这里最终通过app.thread进入到ApplicationThreadProxy的scheduleLaunchActivity函数中,注意,这里的第二个参数r,是一个ActivityRecord类型的Binder对象,用来作来这个Activity的token值。

Step 29. ApplicationThreadProxy.scheduleLaunchActivity

这个函数定义在frameworks/base/core/java/android/app/ApplicationThreadNative.java文件中:

classApplicationThreadProxyimplementsIApplicationThread {

......

publicfinalvoidscheduleLaunchActivity(Intent intent, IBinder token,intident,

ActivityInfo info, Bundle state, List pendingResults,

List pendingNewIntents, booleannotResumed,booleanisForward)

throwsRemoteException {

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();

}

......

}

这个函数最终通过Binder驱动程序进入到ApplicationThread的scheduleLaunchActivity函数中。

Step 30. ApplicationThread.scheduleLaunchActivity

这个函数定义在frameworks/base/core/java/android/app/ActivityThread.java文件中:

publicfinalclassActivityThread {

......

privatefinalclassApplicationThreadextendsApplicationThreadNative {

......

// we use token to identify this activity without having to send the

// activity itself back to the activity manager. (matters more with ipc)

publicfinalvoidscheduleLaunchActivity(Intent intent, IBinder token,intident,

ActivityInfo info, Bundle state, List pendingResults,

List pendingNewIntents, booleannotResumed,booleanisForward) {

ActivityClientRecord r = newActivityClientRecord();

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);

}

......

}

......

}

函数首先创建一个ActivityClientRecord实例,并且初始化它的成员变量,然后调用ActivityThread类的queueOrSendMessage函数进一步处理。

Step 31. ActivityThread.queueOrSendMessage

这个函数定义在frameworks/base/core/java/android/app/ActivityThread.java文件中:

publicfinalclassActivityThread {

......

privatefinalclassApplicationThreadextendsApplicationThreadNative {

......

// if the thread hasn't started yet, we don't have the handler, so just

// save the messages until we're ready.

privatefinalvoidqueueOrSendMessage(intwhat, Object obj) {

queueOrSendMessage(what, obj, 0,0);

}

......

privatefinalvoidqueueOrSendMessage(intwhat, Object obj,intarg1,intarg2) {

synchronized(this) {

......

Message msg = Message.obtain();

msg.what = what;

msg.obj = obj;

msg.arg1 = arg1;

msg.arg2 = arg2;

mH.sendMessage(msg);

}

}

......

}

......

}

函数把消息内容放在msg中,然后通过mH把消息分发出去,这里的成员变量mH我们在前面已经见过,消息分发出去后,最后会调用H类的handleMessage函数。

Step 32. H.handleMessage

这个函数定义在frameworks/base/core/java/android/app/ActivityThread.java文件中:

publicfinalclassActivityThread {

......

privatefinalclassHextendsHandler {

......

publicvoidhandleMessage(Message msg) {

......

switch(msg.what) {

caseLAUNCH_ACTIVITY: {

ActivityClientRecord r = (ActivityClientRecord)msg.obj;

r.packageInfo = getPackageInfoNoCheck(

r.activityInfo.applicationInfo);

handleLaunchActivity(r, null);

} break;

......

}

......

}

......

}

这里最后调用ActivityThread类的handleLaunchActivity函数进一步处理。

Step 33. ActivityThread.handleLaunchActivity

这个函数定义在frameworks/base/core/java/android/app/ActivityThread.java文件中:

publicfinalclassActivityThread {

......

privatefinalvoidhandleLaunchActivity(ActivityClientRecord r, Intent customIntent) {

......

Activity a = performLaunchActivity(r, customIntent);

if(a !=null) {

r.createdConfig = newConfiguration(mConfiguration);

Bundle oldState = r.state;

handleResumeActivity(r.token, false, r.isForward);

......

} else{

......

}

}

......

}

这里首先调用performLaunchActivity函数来加载这个Activity类,即shy.luo.activity.MainActivity,然后调用它的onCreate函数,最后回到handleLaunchActivity函数时,再调用handleResumeActivity函数来使这个Activity进入Resumed状态,即会调用这个Activity的onResume函数,这是遵循Activity的生命周期的。

Step 34. ActivityThread.performLaunchActivity

这个函数定义在frameworks/base/core/java/android/app/ActivityThread.java文件中:

publicfinalclassActivityThread {

......

privatefinalActivity 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 = newComponentName(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 = newContextImpl();

appContext.init(r.packageInfo, r.token, this);

appContext.setOuterContext(activity);

CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());

Configuration config = newConfiguration(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;

inttheme = 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) {

thrownewSuperNotCalledException(

"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) {

......

}

returnactivity;

}

......

}

函数前面是收集要启动的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 = newComponentName(r.activityInfo.packageName,

r.activityInfo.targetActivity);

}

然后通过ClassLoader将shy.luo.activity.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) {

......

}

接下来是创建Application对象,这是根据AndroidManifest.xml配置文件中的Application标签的信息来创建的:

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

后面的代码主要创建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);

最后还要调用MainActivity的onCreate函数:

mInstrumentation.callActivityOnCreate(activity, r.state);

这里不是直接调用MainActivity的onCreate函数,而是通过mInstrumentation的callActivityOnCreate函数来间接调用,前面我们说过,mInstrumentation在这里的作用是监控Activity与系统的交互操作,相当于是系统运行日志。

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可私 6信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 、可私信6博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 、可私信6博主看论文后选择购买源代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值