前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android应用程序启动过程源代码分析(4)

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

作者头像
全栈程序员站长
发布2022-07-05 08:41:51
3480
发布2022-07-05 08:41:51
举报
文章被收录于专栏:全栈程序员必看

Step 28. ActivityStack.realStartActivityLocked

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

  1. public class ActivityStack {
  2. ……
  3. final boolean realStartActivityLocked(ActivityRecord r,
  4. Proce***ecord app, boolean andResume, boolean checkConfig)
  5. throws RemoteException {
  6. ……
  7. r.app = app;
  8. ……
  9. int idx = 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(new Intent(r.intent), r,
  24. System.identityHashCode(r),
  25. r.info, r.icicle, results, newIntents, !andResume,
  26. mService.isNextTransitionForward());
  27. ……
  28. } catch (RemoteException e) {
  29. ……
  30. }
  31. ……
  32. return true;
  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. class ApplicationThreadProxy implements IApplicationThread {
  2. ……
  3. public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
  4. ActivityInfo info, Bundle state, List<ResultInfo> pendingResults,
  5. List<Intent> pendingNewIntents, boolean notResumed, boolean isForward)
  6. throws RemoteException {
  7. Parcel data = 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. public final class ActivityThread {
  2. ……
  3. private final class ApplicationThread extends ApplicationThreadNative {
  4. ……
  5. // we use token to identify this activity without having to send the
  6. // activity itself back to the activity manager. (matters more with ipc)
  7. public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
  8. ActivityInfo info, Bundle state, List<ResultInfo> pendingResults,
  9. List<Intent> pendingNewIntents, boolean notResumed, boolean isForward) {
  10. ActivityClientRecord r = new ActivityClientRecord();
  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. public final class ActivityThread {
  2. ……
  3. private final class ApplicationThread extends ApplicationThreadNative {
  4. ……
  5. // if the thread hasn’t started yet, we don’t have the handler, so just
  6. // save the messages until we’re ready.
  7. private final void queueOrSendMessage(int what, Object obj) {
  8. queueOrSendMessage(what, obj, 0, 0);
  9. }
  10. ……
  11. private final void queueOrSendMessage(int what, Object obj, int arg1, int arg2) {
  12. synchronized (this) {
  13. ……
  14. Message msg = 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. public final class ActivityThread {
  2. ……
  3. private final class H extends Handler {
  4. ……
  5. public void handleMessage(Message msg) {
  6. ……
  7. switch (msg.what) {
  8. case LAUNCH_ACTIVITY: {
  9. ActivityClientRecord r = (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. public final class ActivityThread {
  2. ……
  3. private final void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) {
  4. ……
  5. Activity a = performLaunchActivity(r, customIntent);
  6. if (a != null) {
  7. r.createdConfig = new Configuration(mConfiguration);
  8. Bundle oldState = 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. public final class ActivityThread {
  2. ……
  3. private final Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
  4. ActivityInfo aInfo = r.activityInfo;
  5. if (r.packageInfo == null) {
  6. r.packageInfo = getPackageInfo(aInfo.applicationInfo,
  7. Context.CONTEXT_INCLUDE_CODE);
  8. }
  9. ComponentName component = 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 = new ComponentName(r.activityInfo.packageName,
  17. r.activityInfo.targetActivity);
  18. }
  19. Activity activity = null;
  20. try {
  21. java.lang.ClassLoader cl = 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 (Exception e) {
  29. ……
  30. }
  31. try {
  32. Application app = r.packageInfo.makeApplication(false, mInstrumentation);
  33. ……
  34. if (activity != null) {
  35. ContextImpl appContext = new ContextImpl();
  36. appContext.init(r.packageInfo, r.token, this);
  37. appContext.setOuterContext(activity);
  38. CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
  39. Configuration config = new Configuration(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. int theme = 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. throw new SuperNotCalledException(
  74. “Activity “ + r.intent.getComponent().toShortString() +
  75. ” did not call through to super.onPostCreate()”);
  76. }
  77. }
  78. }
  79. r.paused = true;
  80. mActivities.put(r.token, r);
  81. } catch (SuperNotCalledException e) {
  82. ……
  83. } catch (Exception e) {
  84. ……
  85. }
  86. return activity;
  87. }
  88. ……
  89. }

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

  1. ActivityInfo aInfo = r.activityInfo;
  2. if (r.packageInfo == null) {
  3. r.packageInfo = getPackageInfo(aInfo.applicationInfo,
  4. Context.CONTEXT_INCLUDE_CODE);
  5. }
  6. ComponentName component = 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 = new ComponentName(r.activityInfo.packageName,
  14. r.activityInfo.targetActivity);
  15. }

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

  1. Activity activity = null;
  2. try {
  3. java.lang.ClassLoader cl = 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 (Exception e) {
  11. ……
  12. }

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

  1. Application app = 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与系统的交互操作,相当于是系统运行日志。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/110569.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021年8月1,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档