本文基于Android 28 版本
Activity 的启动过程是从 startActivity 方法开始的,在Activity类中该方法最终调用 startActivityForResult 方法,如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public void startActivityForResult (@RequiresPermission Intent intent, int requestCode, @Nullable Bundle options) { ... if (mParent == null ) { options = transferSpringboardActivityOptions(options); Instrumentation.ActivityResult ar = mInstrumentation.execStartActivity( this , mMainThread.getApplicationThread(), mToken, this , intent, requestCode, options); if (ar != null ) { mMainThread.sendActivityResult( mToken, mEmbeddedID, requestCode, ar.getResultCode(), ar.getResultData()); } } ... }
在该方法中,通过Instrumentation的execStartActivity 方法执行启动流程:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 public ActivityResult execStartActivity ( Context who, IBinder contextThread, IBinder token, Activity target, Intent intent, int requestCode, Bundle options) { IApplicationThread whoThread = (IApplicationThread) contextThread; Uri referrer = target != null ? target.onProvideReferrer() : null ; if (referrer != null ) { intent.putExtra(Intent.EXTRA_REFERRER, referrer); } if (mActivityMonitors != null ) { synchronized (mSync) { final int N = mActivityMonitors.size(); for (int i=0 ; i<N; i++) { final ActivityMonitor am = mActivityMonitors.get(i); ActivityResult result = null ; if (am.ignoreMatchingSpecificIntents()) { result = am.onStartActivity(intent); } if (result != null ) { am.mHits++; return result; } else if (am.match(who, null , intent)) { am.mHits++; if (am.isBlocking()) { return requestCode >= 0 ? am.getResult() : null ; } break ; } } } } try { intent.migrateExtraStreamToClipData(); intent.prepareToLeaveProcess(who); int result = ActivityManager.getService() .startActivity(whoThread, who.getBasePackageName(), intent, intent.resolveTypeIfNeeded(who.getContentResolver()), token, target != null ? target.mEmbeddedID : null , requestCode, 0 , null , options); checkStartActivityResult(result, intent); } catch (RemoteException e) { throw new RuntimeException("Failure from system" , e); } return null ; }
接着上面分析 ActivityManager.getService().startActivity,方法内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public static IActivityManager getService () { return IActivityManagerSingleton.get(); } private static final Singleton<IActivityManager> IActivityManagerSingleton = new Singleton<IActivityManager>() { @Override protected IActivityManager create () { final IBinder b = ServiceManager.getService(Context.ACTIVITY_SERVICE); final IActivityManager am = IActivityManager.Stub.asInterface(b); return am; } };
Android 8.0 以上版本使用AIDL方式实现与ActivityManagerServicer 通信,Android 8.0 一下版本是通过Binder方式实现,详情请查看 ActivityManagerNative 方法。两种方式其实一样,不过使用AIDL 简化了代码结构。Android 中 ActivityManageService 负责系统中所有 Activity 启动和执行,启动 Activity 流程最终是调用ActivityManagerService的startActivity 方法:
1 2 3 4 5 6 7 8 @Override public final int startActivity (IApplicationThread caller, String callingPackage, Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode, int startFlags, ProfilerInfo profilerInfo, Bundle bOptions) { return startActivityAsUser(caller, callingPackage, intent, resolvedType, resultTo, resultWho, requestCode, startFlags, profilerInfo, bOptions, UserHandle.getCallingUserId()); }
Activity
startActivity
startActivityForResult
Instrumentation
execStartActivity
checkStartActivityResult
ActivityManangerService
startActivityAsUser
obtainStarter
ActivityStartController
execute
startActivity
startActivityUnchecked
ActivityStack