这篇不是技术文章,请勿参考 这篇不是技术文章,请勿参考 这篇不是技术文章,请勿参考
Activity的启动流程最初由Zygote发起,通过AMS来启动Activity,其中的过程可以总结为Launcher => AMS,AMS => ApplicationThread,ApplicationThread => Activity
// BaseDraggingActivity源码节选 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) { ...省略部分... // Prepare intent intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); if (v != null) { intent.setSourceBounds(getViewBounds(v)); } try { ...省略部分... if (isShortcut) { // 快捷入口shortcut的处理逻辑 startShortcutIntentSafely(intent, optsBundle, item); } else if (user == null || user.equals(Process.myUserHandle())) { // Activity启动路径 startActivity(intent, optsBundle); } ...省略部分... return false; }
// Activity源码节选 public void startActivityForResult(@RequiresPermission Intent intent, int requestCode, @Nullable Bundle options) { // 这里mParent指的是Activity类型,判断是否存在父Activity if (mParent == null) { options = transferSpringboardActivityOptions(options); Instrumentation.ActivityResult ar = mInstrumentation.execStartActivity( this, mMainThread.getApplicationThread(), mToken, this, intent, requestCode, options); ...省略部分... if (requestCode >= 0) { mStartedActivity = true; } cancelInputsAndStartExitTransition(options); } ...省略部分... } public ActivityResult execStartActivity( Context who, IBinder contextThread, IBinder token, Activity target, Intent intent, int requestCode, Bundle options) { ...省略部分... 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); return null; }
// ActivityManager源码节选 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; } };
总的来看,这一阶段就是Activity及其子类借助Instrumentation来获取AMS实例,可以说应用启动也是通过Activity发起的,而这也就是一个启动Activity的过程。
还行,我还可以
ActivityManagerService源码节选 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(......, UserHandle.getCallingUserId()); } public final int startActivityAsUser(IApplicationThread caller, String callingPackage, Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode, int startFlags, ProfilerInfo profilerInfo, Bundle bOptions, int userId, boolean validateIncomingUser) { // 判断调用者进程是否被隔断,入参是在抛出异常时加入Log的,内容并不影响判断结果 // 内部获取调用者uid后,通过UserHandler来判断uid是否在隔断范围内 enforceNotIsolatedCaller("startActivity"); // 权限判断同样通过ActivityStartController进行封装 userId = mActivityStartController.checkTargetUser(userId, validateIncomingUser, Binder.getCallingPid(), Binder.getCallingUid(), "startActivityAsUser"); return mActivityStartController.obtainStarter(intent, "startActivityAsUser") .setCaller(caller) .setCallingPackage(callingPackage) .setResolvedType(resolvedType) .setResultTo(resultTo) .setResultWho(resultWho) .setRequestCode(requestCode) .setStartFlags(startFlags) .setProfilerInfo(profilerInfo) .setActivityOptions(bOptions) .setMayWait(userId) .execute(); // 建造者模式 }
ActivityStarter源码节选 private int startActivity(IApplicationThread caller, Intent intent, Intent ephemeralIntent, String resolvedType, ActivityInfo aInfo, ResolveInfo rInfo, IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, IBinder resultTo, String resultWho, int requestCode, int callingPid, int callingUid, String callingPackage, int realCallingPid, int realCallingUid, int startFlags, SafeActivityOptions options, boolean ignoreTargetSecurity, boolean componentSpecified, ActivityRecord[] outActivity, TaskRecord inTask, String reason, boolean allowPendingRemoteAnimationRegistryLookup) { // 启动原因为空抛出异常,这里的reason是从AMS传递过来的 if (TextUtils.isEmpty(reason)) { throw new IllegalArgumentException("Need to specify a reason."); } ...省略部分... mLastStartActivityResult = startActivity(caller, intent, ephemeralIntent, resolvedType, aInfo, rInfo, voiceSession, voiceInteractor, resultTo, resultWho, requestCode, callingPid, callingUid, callingPackage, realCallingPid, realCallingUid, startFlags, options, ignoreTargetSecurity, componentSpecified, mLastStartActivityRecord, inTask, allowPendingRemoteAnimationRegistryLookup); ...省略部分... return getExternalResult(mLastStartActivityResult); }
emm…
将一路传递的参数转换为ActivityClientRecord,之后获取packageInfo,加载Activity所属的Apk,之后将Activity状态职位Resume,获取ActivityInfo存储AndroidManifest设置的Activity节点信息,最终通过Instrumentation调起performCreate启动Activity,进入Activity的onCreate。
其实整个阅读流程已经非常清晰了,从最开始信心满满细致入微到逐渐暴躁失去耐心最后彻底掀桌,第一次的源码阅读从知识层面来说并没有什么收获。我也确定了源码不是这么读滴,把源码当成demo读我也是有一手的(叉腰,也可能是我的功力太浅导致的,之后的源码阅读不能这么整了。有问题求解时就仔细观摩一处,单纯膜拜时就放弃细节。啥都想要太消耗精力也太低效了。
ok,历时很久以后终于又沉淀出了一丢丢东西,希望能再接再厉继续年更吧!ヽ(゚∀゚)メ(゚∀゚)ノ