Android应用程序启动过程源代码分析(4)
Step 28. ActivityStack.realStartActivityLocked
这个函数定义在frameworks/base/services/java/com/android/server/am/ActivityStack.java文件中:
- publicclassActivityStack{
- ......
- finalbooleanrealStartActivityLocked(ActivityRecordr,
- ProcessRecordapp,booleanandResume,booleancheckConfig)
- throwsRemoteException{
- ......
- r.app=app;
- ......
- intidx=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(newIntent(r.intent),r,
- System.identityHashCode(r),
- r.info,r.icicle,results,newIntents,!andResume,
- mService.isNextTransitionForward());
- ......
- }catch(RemoteExceptione){
- ......
- }
- ......
- returntrue;
- }
- ......
- }
这里最终通过app.thread进入到ApplicationThreadProxy的scheduleLaunchActivity函数中,注意,这里的第二个参数r,是一个ActivityRecord类型的Binder对象,用来作来这个Activity的token值。
Step 29.ApplicationThreadProxy.scheduleLaunchActivity
这个函数定义在frameworks/base/core/java/android/app/ApplicationThreadNative.java文件中:
- classApplicationThreadProxyimplementsIApplicationThread{
- ......
- publicfinalvoidscheduleLaunchActivity(Intentintent,IBindertoken,intident,
- ActivityInfoinfo,Bundlestate,List<ResultInfo>pendingResults,
- List<Intent>pendingNewIntents,booleannotResumed,booleanisForward)
- throwsRemoteException{
- Parceldata=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{
- ......
- //weusetokentoidentifythisactivitywithouthavingtosendthe
- //activityitselfbacktotheactivitymanager.(mattersmorewithipc)
- publicfinalvoidscheduleLaunchActivity(Intentintent,IBindertoken,intident,
- ActivityInfoinfo,Bundlestate,List<ResultInfo>pendingResults,
- List<Intent>pendingNewIntents,booleannotResumed,booleanisForward){
- ActivityClientRecordr=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{
- ......
- //ifthethreadhasn'tstartedyet,wedon'thavethehandler,sojust
- //savethemessagesuntilwe'reready.
- privatefinalvoidqueueOrSendMessage(intwhat,Objectobj){
- queueOrSendMessage(what,obj,0,0);
- }
- ......
- privatefinalvoidqueueOrSendMessage(intwhat,Objectobj,intarg1,intarg2){
- synchronized(this){
- ......
- Messagemsg=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(Messagemsg){
- ......
- switch(msg.what){
- caseLAUNCH_ACTIVITY:{
- ActivityClientRecordr=(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(ActivityClientRecordr,IntentcustomIntent){
- ......
- Activitya=performLaunchActivity(r,customIntent);
- if(a!=null){
- r.createdConfig=newConfiguration(mConfiguration);
- BundleoldState=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{
- ......
- privatefinalActivityperformLaunchActivity(ActivityClientRecordr,IntentcustomIntent){
- ActivityInfoaInfo=r.activityInfo;
- if(r.packageInfo==null){
- r.packageInfo=getPackageInfo(aInfo.applicationInfo,
- Context.CONTEXT_INCLUDE_CODE);
- }
- ComponentNamecomponent=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);
- }
- Activityactivity=null;
- try{
- java.lang.ClassLoadercl=r.packageInfo.getClassLoader();
- activity=mInstrumentation.newActivity(
- cl,component.getClassName(),r.intent);
- r.intent.setExtrasClassLoader(cl);
- if(r.state!=null){
- r.state.setClassLoader(cl);
- }
- }catch(Exceptione){
- ......
- }
- try{
- Applicationapp=r.packageInfo.makeApplication(false,mInstrumentation);
- ......
- if(activity!=null){
- ContextImplappContext=newContextImpl();
- appContext.init(r.packageInfo,r.token,this);
- appContext.setOuterContext(activity);
- CharSequencetitle=r.activityInfo.loadLabel(appContext.getPackageManager());
- Configurationconfig=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()+
- "didnotcallthroughtosuper.onPostCreate()");
- }
- }
- }
- r.paused=true;
- mActivities.put(r.token,r);
- }catch(SuperNotCalledExceptione){
- ......
- }catch(Exceptione){
- ......
- }
- returnactivity;
- }
- ......
- }
函数前面是收集要启动的Activity的相关信息,主要package和component信息:
- ActivityInfoaInfo=r.activityInfo;
- if(r.packageInfo==null){
- r.packageInfo=getPackageInfo(aInfo.applicationInfo,
- Context.CONTEXT_INCLUDE_CODE);
- }
- ComponentNamecomponent=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类加载进来:
- Activityactivity=null;
- try{
- java.lang.ClassLoadercl=r.packageInfo.getClassLoader();
- activity=mInstrumentation.newActivity(
- cl,component.getClassName(),r.intent);
- r.intent.setExtrasClassLoader(cl);
- if(r.state!=null){
- r.state.setClassLoader(cl);
- }
- }catch(Exceptione){
- ......
- }
接下来是创建Application对象,这是根据AndroidManifest.xml配置文件中的Application标签的信息来创建的:
- Applicationapp=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与系统的交互操作,相当于是系统运行日志。
更多相关文章
- C语言函数以及函数的使用
- Android 各种自定义进度条Progressbar
- Android读取Txt文件
- Android文件递归遍历
- android 不使用布局文件,完全由代码控制布局实例
- Momo自定义DialogFragment
- 自定义Android editText
- Android 删除SD卡文件和文件及创建文件夹和文件
- android 读写文件数据