Step 28. ActivityStack.realStartActivityLocked

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

        
  1. publicclassActivityStack{
  2. ......
  3. finalbooleanrealStartActivityLocked(ActivityRecordr,
  4. ProcessRecordapp,booleanandResume,booleancheckConfig)
  5. throwsRemoteException{
  6. ......
  7. r.app=app;
  8. ......
  9. intidx=app.activities.indexOf(r);
  10. if(idx<0){
  11. app.activities.add(r);
  12. }
  13. ......
  14. try{
  15. ......
  16. List<ResultInfo>results=null;
  17. List<Intent>newIntents=null;
  18. if(andResume){
  19. results=r.results;
  20. newIntents=r.newIntents;
  21. }
  22. ......
  23. app.thread.scheduleLaunchActivity(newIntent(r.intent),r,
  24. System.identityHashCode(r),
  25. r.info,r.icicle,results,newIntents,!andResume,
  26. mService.isNextTransitionForward());
  27. ......
  28. }catch(RemoteExceptione){
  29. ......
  30. }
  31. ......
  32. returntrue;
  33. }
  34. ......
  35. }

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

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

        
  1. classApplicationThreadProxyimplementsIApplicationThread{
  2. ......
  3. publicfinalvoidscheduleLaunchActivity(Intentintent,IBindertoken,intident,
  4. ActivityInfoinfo,Bundlestate,List<ResultInfo>pendingResults,
  5. List<Intent>pendingNewIntents,booleannotResumed,booleanisForward)
  6. throwsRemoteException{
  7. Parceldata=Parcel.obtain();
  8. data.writeInterfaceToken(IApplicationThread.descriptor);
  9. intent.writeToParcel(data,0);
  10. data.writeStrongBinder(token);
  11. data.writeInt(ident);
  12. info.writeToParcel(data,0);
  13. data.writeBundle(state);
  14. data.writeTypedList(pendingResults);
  15. data.writeTypedList(pendingNewIntents);
  16. data.writeInt(notResumed?1:0);
  17. data.writeInt(isForward?1:0);
  18. mRemote.transact(SCHEDULE_LAUNCH_ACTIVITY_TRANSACTION,data,null,
  19. IBinder.FLAG_ONEWAY);
  20. data.recycle();
  21. }
  22. ......
  23. }

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

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

        
  1. publicfinalclassActivityThread{
  2. ......
  3. privatefinalclassApplicationThreadextendsApplicationThreadNative{
  4. ......
  5. //weusetokentoidentifythisactivitywithouthavingtosendthe
  6. //activityitselfbacktotheactivitymanager.(mattersmorewithipc)
  7. publicfinalvoidscheduleLaunchActivity(Intentintent,IBindertoken,intident,
  8. ActivityInfoinfo,Bundlestate,List<ResultInfo>pendingResults,
  9. List<Intent>pendingNewIntents,booleannotResumed,booleanisForward){
  10. ActivityClientRecordr=newActivityClientRecord();
  11. r.token=token;
  12. r.ident=ident;
  13. r.intent=intent;
  14. r.activityInfo=info;
  15. r.state=state;
  16. r.pendingResults=pendingResults;
  17. r.pendingIntents=pendingNewIntents;
  18. r.startsNotResumed=notResumed;
  19. r.isForward=isForward;
  20. queueOrSendMessage(H.LAUNCH_ACTIVITY,r);
  21. }
  22. ......
  23. }
  24. ......
  25. }

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

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

        
  1. publicfinalclassActivityThread{
  2. ......
  3. privatefinalclassApplicationThreadextendsApplicationThreadNative{
  4. ......
  5. //ifthethreadhasn'tstartedyet,wedon'thavethehandler,sojust
  6. //savethemessagesuntilwe'reready.
  7. privatefinalvoidqueueOrSendMessage(intwhat,Objectobj){
  8. queueOrSendMessage(what,obj,0,0);
  9. }
  10. ......
  11. privatefinalvoidqueueOrSendMessage(intwhat,Objectobj,intarg1,intarg2){
  12. synchronized(this){
  13. ......
  14. Messagemsg=Message.obtain();
  15. msg.what=what;
  16. msg.obj=obj;
  17. msg.arg1=arg1;
  18. msg.arg2=arg2;
  19. mH.sendMessage(msg);
  20. }
  21. }
  22. ......
  23. }
  24. ......
  25. }

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

Step 32. H.handleMessage

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

        
  1. publicfinalclassActivityThread{
  2. ......
  3. privatefinalclassHextendsHandler{
  4. ......
  5. publicvoidhandleMessage(Messagemsg){
  6. ......
  7. switch(msg.what){
  8. caseLAUNCH_ACTIVITY:{
  9. ActivityClientRecordr=(ActivityClientRecord)msg.obj;
  10. r.packageInfo=getPackageInfoNoCheck(
  11. r.activityInfo.applicationInfo);
  12. handleLaunchActivity(r,null);
  13. }break;
  14. ......
  15. }
  16. ......
  17. }
  18. ......
  19. }

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

Step 33.ActivityThread.handleLaunchActivity

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

        
  1. publicfinalclassActivityThread{
  2. ......
  3. privatefinalvoidhandleLaunchActivity(ActivityClientRecordr,IntentcustomIntent){
  4. ......
  5. Activitya=performLaunchActivity(r,customIntent);
  6. if(a!=null){
  7. r.createdConfig=newConfiguration(mConfiguration);
  8. BundleoldState=r.state;
  9. handleResumeActivity(r.token,false,r.isForward);
  10. ......
  11. }else{
  12. ......
  13. }
  14. }
  15. ......
  16. }

这里首先调用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文件中:

        
  1. publicfinalclassActivityThread{
  2. ......
  3. privatefinalActivityperformLaunchActivity(ActivityClientRecordr,IntentcustomIntent){
  4. ActivityInfoaInfo=r.activityInfo;
  5. if(r.packageInfo==null){
  6. r.packageInfo=getPackageInfo(aInfo.applicationInfo,
  7. Context.CONTEXT_INCLUDE_CODE);
  8. }
  9. ComponentNamecomponent=r.intent.getComponent();
  10. if(component==null){
  11. component=r.intent.resolveActivity(
  12. mInitialApplication.getPackageManager());
  13. r.intent.setComponent(component);
  14. }
  15. if(r.activityInfo.targetActivity!=null){
  16. component=newComponentName(r.activityInfo.packageName,
  17. r.activityInfo.targetActivity);
  18. }
  19. Activityactivity=null;
  20. try{
  21. java.lang.ClassLoadercl=r.packageInfo.getClassLoader();
  22. activity=mInstrumentation.newActivity(
  23. cl,component.getClassName(),r.intent);
  24. r.intent.setExtrasClassLoader(cl);
  25. if(r.state!=null){
  26. r.state.setClassLoader(cl);
  27. }
  28. }catch(Exceptione){
  29. ......
  30. }
  31. try{
  32. Applicationapp=r.packageInfo.makeApplication(false,mInstrumentation);
  33. ......
  34. if(activity!=null){
  35. ContextImplappContext=newContextImpl();
  36. appContext.init(r.packageInfo,r.token,this);
  37. appContext.setOuterContext(activity);
  38. CharSequencetitle=r.activityInfo.loadLabel(appContext.getPackageManager());
  39. Configurationconfig=newConfiguration(mConfiguration);
  40. ......
  41. activity.attach(appContext,this,getInstrumentation(),r.token,
  42. r.ident,app,r.intent,r.activityInfo,title,r.parent,
  43. r.embeddedID,r.lastNonConfigurationInstance,
  44. r.lastNonConfigurationChildInstances,config);
  45. if(customIntent!=null){
  46. activity.mIntent=customIntent;
  47. }
  48. r.lastNonConfigurationInstance=null;
  49. r.lastNonConfigurationChildInstances=null;
  50. activity.mStartedActivity=false;
  51. inttheme=r.activityInfo.getThemeResource();
  52. if(theme!=0){
  53. activity.setTheme(theme);
  54. }
  55. activity.mCalled=false;
  56. mInstrumentation.callActivityOnCreate(activity,r.state);
  57. ......
  58. r.activity=activity;
  59. r.stopped=true;
  60. if(!r.activity.mFinished){
  61. activity.performStart();
  62. r.stopped=false;
  63. }
  64. if(!r.activity.mFinished){
  65. if(r.state!=null){
  66. mInstrumentation.callActivityOnRestoreInstanceState(activity,r.state);
  67. }
  68. }
  69. if(!r.activity.mFinished){
  70. activity.mCalled=false;
  71. mInstrumentation.callActivityOnPostCreate(activity,r.state);
  72. if(!activity.mCalled){
  73. thrownewSuperNotCalledException(
  74. "Activity"+r.intent.getComponent().toShortString()+
  75. "didnotcallthroughtosuper.onPostCreate()");
  76. }
  77. }
  78. }
  79. r.paused=true;
  80. mActivities.put(r.token,r);
  81. }catch(SuperNotCalledExceptione){
  82. ......
  83. }catch(Exceptione){
  84. ......
  85. }
  86. returnactivity;
  87. }
  88. ......
  89. }

函数前面是收集要启动的Activity的相关信息,主要package和component信息:

        
  1. ActivityInfoaInfo=r.activityInfo;
  2. if(r.packageInfo==null){
  3. r.packageInfo=getPackageInfo(aInfo.applicationInfo,
  4. Context.CONTEXT_INCLUDE_CODE);
  5. }
  6. ComponentNamecomponent=r.intent.getComponent();
  7. if(component==null){
  8. component=r.intent.resolveActivity(
  9. mInitialApplication.getPackageManager());
  10. r.intent.setComponent(component);
  11. }
  12. if(r.activityInfo.targetActivity!=null){
  13. component=newComponentName(r.activityInfo.packageName,
  14. r.activityInfo.targetActivity);
  15. }

然后通过ClassLoader将shy.luo.activity.MainActivity类加载进来:

        
  1. Activityactivity=null;
  2. try{
  3. java.lang.ClassLoadercl=r.packageInfo.getClassLoader();
  4. activity=mInstrumentation.newActivity(
  5. cl,component.getClassName(),r.intent);
  6. r.intent.setExtrasClassLoader(cl);
  7. if(r.state!=null){
  8. r.state.setClassLoader(cl);
  9. }
  10. }catch(Exceptione){
  11. ......
  12. }

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

        
  1. Applicationapp=r.packageInfo.makeApplication(false,mInstrumentation);

后面的代码主要创建Activity的上下文信息,并通过attach方法将这些上下文信息设置到MainActivity中去:

        
  1. activity.attach(appContext,this,getInstrumentation(),r.token,
  2. r.ident,app,r.intent,r.activityInfo,title,r.parent,
  3. r.embeddedID,r.lastNonConfigurationInstance,
  4. r.lastNonConfigurationChildInstances,config);

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

        
  1. mInstrumentation.callActivityOnCreate(activity,r.state);

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

更多相关文章

  1. C语言函数以及函数的使用
  2. Android 各种自定义进度条Progressbar
  3. Android读取Txt文件
  4. Android文件递归遍历
  5. android 不使用布局文件,完全由代码控制布局实例
  6. Momo自定义DialogFragment
  7. 自定义Android editText
  8. Android 删除SD卡文件和文件及创建文件夹和文件
  9. android 读写文件数据

随机推荐

  1. Android(安卓)4.2一些变动
  2. 编写高效Android代码
  3. 使用Genymotion时出现INSTALL_FAILED_CPU
  4. Fragment封装切换
  5. android自动化测试工具之DroidPilot
  6. Android面面观——Android事件处理下(按键
  7. Android(安卓)设计模式之面向对象的六大
  8. android 搜索框(一)
  9. 【翻译】Android安全之Linux内核篇
  10. android 退出按钮