应用进程内
如何使用 Intent 做 Activity 的跳转
- Intnet intent = new Intent(MainActivity.this,TestActivity.class);
- startActivity(intent);
我们通常会这样写, 就能跳转到 TestActivity 中, 但是你知道这简简单单的两行代码在我们庞大的安卓系统中经历了怎样的加工处理吗? 接下来我带大家深入到系统内了解整个的启动过程
这里调用了 startActivity 的函数, 这个函数被声明在了 Context 类中, 然后我们的 Activity 类有重写版本, 所以会执行 Activity 类下的版本, 我们跟进. 大家准备好了吗. 我要发车了!
- @Override
- public void startActivity(Intent intent) {
- this.startActivity(intent, null);
- }
- @Override
- public void startActivity(Intent intent, @Nullable Bundle options) {
- if (options != null) {
- startActivityForResult(intent, -1, options);
- } else {
- startActivityForResult(intent, -1);
- }
- }
- public void startActivityForResult(@RequiresPermission Intent intent, int requestCode) {
- startActivityForResult(intent, requestCode, null);
- }
- 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());
- }
- if (requestCode>= 0) {
- mStartedActivity = true;
- }
- cancelInputsAndStartExitTransition(options);
- } else {
- if (options != null) {
- mParent.startActivityFromChild(this, intent, requestCode, options);
- } else {
- // Note we want to go through this method for compatibility with
- // existing applications that may have overridden it.
- mParent.startActivityFromChild(this, intent, requestCode);
- }
- }
- }
上面有四个函数, 基本上都是简单的判断和一些代码的封装, 在最后一个函数中, 执行了 mInstrumentation 对象下的 execStartActivity 函数, 这个函数会做一些脱离应用进程的操作, 准备通过 IBinder 机制将请求发送到 Ams 中. 简单说一下传入参数
this 指的就是我们的 MainActivity 对象, 标明谁是发起者
getApplicationThread 获取的是应用标示, 因为 Android 系统中每一个应用都是一个客户, 而 Ams 更像是柜台服务人员
mToken 是系统的一个内部标示, 是 ActivityRecord 类下的一个静态内部类, 里边用弱引用存了一个 ActivityRecord 对象, 会在 startActivityForResult 函数中应用, 标明应该调用谁的 onActivityResult 函数, 后面会有讲到
同第一个参数, 标明了发起者是谁
intent 就略过吧, 很常用了
requestCode 这个值也很常用, 所以也不做详细介绍了, 默认 - 1
options 是我们跳转页面需要携带的参数, 当然没有参数自然也会是 null 了
接下来我们了解完参数后看一下内部实现吧
- 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;
- }
这个实现就是执行 intent 脱离我们的应用线程, 然后发送 IBinder 消息 (俗称 IPC) 到 Ams 中, 到此处便结束了应用之旅, 开始进入系统内部.
另外我在补充一下 ActivityMonitor 这个类的知识, 这个是一个 Activity 的监视器, 可以在写测试类的时候用到, 通常是写一个测试类, 然后继承 InstrumentationTestCase, 有兴趣的可以试一下
系统进程内(system_server)
- @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());
- }
这个函数定义在 Ams(ActivityManagerService), 因为是对外调用的接口, 所以为了不做过多的逻辑处理, 紧接着转发给了 startActivityAsUser 函数校验一下发起者身份. 我们可以看到其余的参数都是透传过来的, 但是最后多出来了一个 UserId
UserId: 手机上的用户标示, 类似于电脑多用户, 通过 应用 uid / 100000 计算得出, 0 为手机持有者, 默认值为 0
Uid: 应用 id, 普通用户从 10000
Pid: 进程 id
我们继续向下看吧
- @Override
- 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) {
- enforceNotIsolatedCaller("startActivity");
- userId = mUserController.handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(),
- userId, false, ALLOW_FULL_ONLY, "startActivity", null);
- // TODO: Switch to user App stacks here.
- return mActivityStarter.startActivityMayWait(caller, -1, callingPackage, intent,
- resolvedType, null, null, resultTo, resultWho, requestCode, startFlags,
- profilerInfo, null, null, bOptions, false, userId, null, "startActivityAsUser");
- }
通过了 mUserController 这个对象下的 handleIncomingUser 函数校验, 这个函数会自己在通过 UserHandle.getCallingUserId()获取一遍 userid, 与传入参数的 userId 做校验, 如果一支便返回, 所以我们用普通用户的话, 这个 userId 便是 0 了. 随后转交给了 ActivityStarter 这个类来正式执行开启过程, 首先第一站便是 startActivityMayWait 函数, 接下来如果函数过长, 我会在代码上加注释, 方便阅读.
这个函数的参数也开始变得非常多了. 因为后续涉及到的功能太广了, 我会把我所知的都写出来分享给大家
- Instant App
- final int startActivityMayWait(IApplicationThread caller, int callingUid,
- String callingPackage, Intent intent, String resolvedType,
- IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
- IBinder resultTo, String resultWho, int requestCode, int startFlags,
- ProfilerInfo profilerInfo, WaitResult outResult,
- Configuration globalConfig, Bundle bOptions, boolean ignoreTargetSecurity, int userId,
- TaskRecord inTask, String reason) {
- if (intent != null && intent.hasFileDescriptors()) {
- throw new IllegalArgumentException("File descriptors passed in Intent");
- }
- mSupervisor.mActivityMetricsLogger.notifyActivityLaunching();
- boolean componentSpecified = intent.getComponent() != null;
- // Save a copy in case ephemeral needs it
- final Intent ephemeralIntent = new Intent(intent);
- // Don't modify the client's object!
- intent = new Intent(intent);
- // 判断是否为 instant App,instant App 可以通过 URL 打开, 不需要 component, 所以置为 null
- // 关于 instant App, 大家可以去查询一下资料哦
- if (componentSpecified
- && intent.getData() != null
- && Intent.ACTION_VIEW.equals(intent.getAction())
- && mService.getPackageManagerInternalLocked()
- .isInstantAppInstallerComponent(intent.getComponent())) {
- // intercept intents targeted directly to the ephemeral installer the
- // ephemeral installer should never be started with a raw URL; instead
- // adjust the intent so it looks like a "normal" instant App launch
- intent.setComponent(null /*component*/);
- componentSpecified = false;
- }
- // 通过 Pms(PackageManagerService), 并且通过我们传递过来的 intent 去查询目标页面的 R
- //esolveInfo. 这个类里放了 ActivityInfo,ServiceInfo,ProviderInfo, 大家可以在
- //demo 中通过 PackageManager.resolveActivity 函数来获取该对象
- ResolveInfo rInfo = mSupervisor.resolveIntent(intent, resolvedType, userId);
- if (rInfo == null) {
- UserInfo userInfo = mSupervisor.getUserInfo(userId);
- if (userInfo != null && userInfo.isManagedProfile()) {
- // Special case for managed profiles, if attempting to launch non-cryto aware
- // App in a locked managed profile from an unlocked parent allow it to resolve
- // as user will be sent via confirm credentials to unlock the profile.
- UserManager userManager = UserManager.get(mService.mContext);
- boolean profileLockedAndParentUnlockingOrUnlocked = false;
- long token = Binder.clearCallingIdentity();
- try {
- UserInfo parent = userManager.getProfileParent(userId);
- profileLockedAndParentUnlockingOrUnlocked = (parent != null)
- && userManager.isUserUnlockingOrUnlocked(parent.id)
- && !userManager.isUserUnlockingOrUnlocked(userId);
- } finally {
- Binder.restoreCallingIdentity(token);
- }
- if (profileLockedAndParentUnlockingOrUnlocked) {
- rInfo = mSupervisor.resolveIntent(intent, resolvedType, userId,
- PackageManager.MATCH_DIRECT_BOOT_AWARE
- | PackageManager.MATCH_DIRECT_BOOT_UNAWARE);
- }
- }
- }
- // 这块便是通过解析 ResolveInfo, 得到 ActivityInfo 对象, 这个 ActivityInfo 大家应该比较熟悉了. 就是我们开发中偶尔也会用到 ActivityInfo
- ActivityInfo aInfo = mSupervisor.resolveActivity(intent, rInfo, startFlags, profilerInfo);
- // 这个便是通过解析 bundle, 查看是否有对 activity 的启动要求, 比如动画之类附加属性,
- // 当然了, 我们的 bundle 都是 null 了, options 自然也会是 null
- ActivityOptions options = ActivityOptions.fromBundle(bOptions);
- synchronized (mService) {
- // 这块通过 Binder 获取到了真实的 pid 和 uid, 便把行参中的 uid 和 pid 置为了 - 1
- // 因为 callingUid 在在调用该方法的时候被传入了 - 1, 大家可以去上边仔细看看
- // caller 是不会为空的. 它是我们的应用标示, IApplicationThread 对象
- final int realCallingPid = Binder.getCallingPid();
- final int realCallingUid = Binder.getCallingUid();
- int callingPid;
- if (callingUid>= 0) {
- callingPid = -1;
- } else if (caller == null) {
- callingPid = realCallingPid;
- callingUid = realCallingUid;
- } else {
- callingPid = callingUid = -1;
- }
- final ActivityStack stack = mSupervisor.mFocusedStack;
- stack.mConfigWillChange = globalConfig != null
- && mService.getGlobalConfiguration().diff(globalConfig) != 0;
- final long origId = Binder.clearCallingIdentity();
- // 这块是一个系统级别的判断, 我们普通的应用是没有权限使用 PRIVATE_FLAG_CANT_SAVE_STATE 这个标记的, 这块是对重量级线程的处理过程, 由于是系统级别的处理, 我就没仔细研读, 抱歉啦~大概意思便是替换了 intent, 换上了新的配置
- if (aInfo != null &&
- (aInfo.applicationInfo.privateFlags
- & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
- // This may be a heavy-weight process! Check to see if we already
- // have another, different heavy-weight process running.
- if (aInfo.processName.equals(aInfo.applicationInfo.packageName)) {
- final ProcessRecord heavy = mService.mHeavyWeightProcess;
- if (heavy != null && (heavy.info.uid != aInfo.applicationInfo.uid
- || !heavy.processName.equals(aInfo.processName))) {
- int appCallingUid = callingUid;
- if (caller != null) {
- ProcessRecord callerApp = mService.getRecordForAppLocked(caller);
- if (callerApp != null) {
- appCallingUid = callerApp.info.uid;
- } else {
- Slog.w(TAG, "Unable to find app for caller" + caller
- + "(pid=" + callingPid + ") when starting:"
- + intent.toString());
- ActivityOptions.abort(options);
- return ActivityManager.START_PERMISSION_DENIED;
- }
- }
- IIntentSender target = mService.getIntentSenderLocked(
- ActivityManager.INTENT_SENDER_ACTIVITY, "android",
- appCallingUid, userId, null, null, 0, new Intent[] { intent },
- new String[] { resolvedType }, PendingIntent.FLAG_CANCEL_CURRENT
- | PendingIntent.FLAG_ONE_SHOT, null);
- Intent newIntent = new Intent();
- if (requestCode>= 0) {
- // Caller is requesting a result.
- newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_HAS_RESULT, true);
- }
- newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_INTENT,
- new IntentSender(target));
- if (heavy.activities.size()> 0) {
- ActivityRecord hist = heavy.activities.get(0);
- newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_APP,
- hist.packageName);
- newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_CUR_TASK,
- hist.getTask().taskId);
- }
- newIntent.putExtra(HeavyWeightSwitcherActivity.KEY_NEW_APP,
- aInfo.packageName);
- newIntent.setFlags(intent.getFlags());
- newIntent.setClassName("android",
- HeavyWeightSwitcherActivity.class.getName());
- intent = newIntent;
- resolvedType = null;
- caller = null;
- callingUid = Binder.getCallingUid();
- callingPid = Binder.getCallingPid();
- componentSpecified = true;
- rInfo = mSupervisor.resolveIntent(intent, null /*resolvedType*/, userId);
- aInfo = rInfo != null ? rInfo.activityInfo : null;
- if (aInfo != null) {
- aInfo = mService.getActivityInfoForUser(aInfo, userId);
- }
- }
- }
- }
- // 此处创建一个 ActivityRecord 数组, 然后 startActivityLocked 会填充上 0 索引, 以便于执行完了 startActivityLocked 函数后保存对象并作后边的处理工作, 这个小技巧大家也可以在日常开发中用到, 并不是只有返回值哦~
- // 我们可以去下一站了, 去 startActivityLocked 内了
- final ActivityRecord[] outRecord = new ActivityRecord[1];
- int res = startActivityLocked(caller, intent, ephemeralIntent, resolvedType,
- aInfo, rInfo, voiceSession, voiceInteractor,
- resultTo, resultWho, requestCode, callingPid,
- callingUid, callingPackage, realCallingPid, realCallingUid, startFlags,
- options, ignoreTargetSecurity, componentSpecified, outRecord, inTask,
- reason);
- Binder.restoreCallingIdentity(origId);
- if (stack.mConfigWillChange) {
- // If the caller also wants to switch to a new configuration,
- // do so now. This allows a clean switch, as we are waiting
- // for the current activity to pause (so we will not destroy
- // it), and have not yet started the next activity.
- mService.enforceCallingPermission(Android.Manifest.permission.CHANGE_CONFIGURATION,
- "updateConfiguration()");
- stack.mConfigWillChange = false;
- if (DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
- "Updating to new configuration after starting activity.");
- mService.updateConfigurationLocked(globalConfig, null, false);
- }
- if (outResult != null) {
- outResult.result = res;
- if (res == ActivityManager.START_SUCCESS) {
- mSupervisor.mWaitingActivityLaunched.add(outResult);
- do {
- try {
- mService.wait();
- } catch (InterruptedException e) {
- }
- } while (outResult.result != START_TASK_TO_FRONT
- && !outResult.timeout && outResult.who == null);
- if (outResult.result == START_TASK_TO_FRONT) {
- res = START_TASK_TO_FRONT;
- }
- }
- if (res == START_TASK_TO_FRONT) {
- final ActivityRecord r = outRecord[0];
- // ActivityRecord may represent a different activity, but it should not be in
- // the resumed state.
- if (r.nowVisible && r.state == RESUMED) {
- outResult.timeout = false;
- outResult.who = r.realActivity;
- outResult.totalTime = 0;
- outResult.thisTime = 0;
- } else {
- outResult.thisTime = SystemClock.uptimeMillis();
- mSupervisor.waitActivityVisible(r.realActivity, outResult);
- // Note: the timeout variable is not currently not ever set.
- do {
- try {
- mService.wait();
- } catch (InterruptedException e) {
- }
- } while (!outResult.timeout && outResult.who == null);
- }
- }
- }
- mSupervisor.mActivityMetricsLogger.notifyActivityLaunched(res, outRecord[0]);
- return res;
- }
- }
虽然有很多注释, 看完这个方法我还是在做一个总结吧
这个函数做了两件重要的事情, 一个是获取 ResolveInfo, 一个是获取 ActivityInfo, 就是目标 Activity 的基本信息. 我们继续研读 startActivityLocked 函数吧
- int startActivityLocked(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,
- ActivityOptions options, boolean ignoreTargetSecurity, boolean componentSpecified,
- ActivityRecord[] outActivity, TaskRecord inTask, String reason) {
- if (TextUtils.isEmpty(reason)) {
- throw new IllegalArgumentException("Need to specify a reason.");
- }
- // 记录时间, 然后转发请求
- mLastStartReason = reason;
- mLastStartActivityTimeMs = System.currentTimeMillis();
- mLastStartActivityRecord[0] = null;
- // 跳入 startActivity 函数中
- mLastStartActivityResult = startActivity(caller, intent, ephemeralIntent, resolvedType,
- aInfo, rInfo, voiceSession, voiceInteractor, resultTo, resultWho, requestCode,
- callingPid, callingUid, callingPackage, realCallingPid, realCallingUid, startFlags,
- options, ignoreTargetSecurity, componentSpecified, mLastStartActivityRecord,
- inTask);
- if (outActivity != null) {
- // mLastStartActivityRecord[0] is set in the call to startActivity above.
- outActivity[0] = mLastStartActivityRecord[0];
- }
- // Aborted results are treated as successes externally, but we must track them internally.
- return mLastStartActivityResult != START_ABORTED ? mLastStartActivityResult : START_SUCCESS;
- }
这一站仅仅是记录时间, 然后清空 mLastStartActivityRecord 数组的位置, 为即将要启动的 Activity 腾位置, 我们继续向下看 startActivity 吧, 又是一个很长的函数~预警预警
- 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,
- ActivityOptions options, boolean ignoreTargetSecurity, boolean componentSpecified,
- ActivityRecord[] outActivity, TaskRecord inTask) {
- int err = ActivityManager.START_SUCCESS;
- // Pull the optional Ephemeral Installer-only bundle out of the options early.
- final Bundle verificationBundle
- = options != null ? options.popAppVerificationBundle() : null;
- // 获取我们应用的进程描述类 ProcessRecord 对象, 获取原理是先获取所有进程
- // 然后用我们的 caller 和 ProcessRecord 下的 thread 对象做对比, 如果是同一个便可以作为返回结果返回出来了
- ProcessRecord callerApp = null;
- if (caller != null) {
- callerApp = mService.getRecordForAppLocked(caller);
- if (callerApp != null) {
- callingPid = callerApp.pid;
- callingUid = callerApp.info.uid;
- } else {
- Slog.w(TAG, "Unable to find app for caller" + caller
- + "(pid=" + callingPid + ") when starting:"
- + intent.toString());
- err = ActivityManager.START_PERMISSION_DENIED;
- }
- }
- // 又一次获取了 userId~~~
- final int userId = aInfo != null ? UserHandle.getUserId(aInfo.applicationInfo.uid) : 0;
- if (err == ActivityManager.START_SUCCESS) {
- Slog.i(TAG, "START u" + userId + "{" + intent.toShortString(true, true, true, false)
- + "} from uid" + callingUid);
- }
- // 这个 resultTo 就是我们的 mToken 对象, 然后 isInAnyStackLocked 的作用便是获取 Token
- // 上弱引用的 ActivityRecord 对象, 这样就得到了我们启动者的 ActivityRecord 对象了, 就是 // 我们例子中的 MainActivity 的 ActivityRecord 对象
- // 这块是处理 startActivityForResult 的逻辑, resultRecord 是哪个 Activity 的信息, 就会调用哪个 Activity 的 onActivityResult 函数, 在 requestCode 大于等于 0 的情况, 它存的是我们的 MainActivity
- ActivityRecord sourceRecord = null;
- ActivityRecord resultRecord = null;
- if (resultTo != null) {
- sourceRecord = mSupervisor.isInAnyStackLocked(resultTo);
- if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
- "Will send result to" + resultTo + " " + sourceRecord);
- if (sourceRecord != null) {
- if (requestCode>= 0 && !sourceRecord.finishing) {
- resultRecord = sourceRecord;
- }
- }
- }
- final int launchFlags = intent.getFlags();
- //FLAG_ACTIVITY_FORWARD_RESULT 这个标记允许我们把 startActivityForResult 的线路
- // 拉长, 比如 A->B->C, 然后 C 可以直接把值传递到 A
- if ((launchFlags & Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0 && sourceRecord != null) {
- // Transfer the result target from the source activity to the new
- // one being started, including any failures.
- if (requestCode>= 0) {
- ActivityOptions.abort(options);
- return ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
- }
- resultRecord = sourceRecord.resultTo;
- if (resultRecord != null && !resultRecord.isInStackLocked()) {
- resultRecord = null;
- }
- resultWho = sourceRecord.resultWho;
- requestCode = sourceRecord.requestCode;
- sourceRecord.resultTo = null;
- if (resultRecord != null) {
- resultRecord.removeResultsLocked(sourceRecord, resultWho, requestCode);
- }
- if (sourceRecord.launchedFromUid == callingUid) {
- // The new activity is being launched from the same uid as the previous
- // activity in the flow, and asking to forward its result back to the
- // previous. In this case the activity is serving as a trampoline between
- // the two, so we also want to update its launchedFromPackage to be the
- // same as the previous activity. Note that this is safe, since we know
- // these two packages come from the same uid; the caller could just as
- // well have supplied that same package name itself. This specifially
- // deals with the case of an intent picker/chooser being launched in the App
- // flow to redirect to an activity picked by the user, where we want the final
- // activity to consider it to have been launched by the previous App activity.
- callingPackage = sourceRecord.launchedFromPackage;
- }
- }
- // 接下来开始做一些校验判断
- if (err == ActivityManager.START_SUCCESS && intent.getComponent() == null) {
- // We couldn't find a class that can handle the given Intent.
- // That's the end of that!
- err = ActivityManager.START_INTENT_NOT_RESOLVED;
- }
- if (err == ActivityManager.START_SUCCESS && aInfo == null) {
- // We couldn't find the specific class specified in the Intent.
- // Also the end of the line.
- err = ActivityManager.START_CLASS_NOT_FOUND;
- }
- if (err == ActivityManager.START_SUCCESS && sourceRecord != null
- && sourceRecord.getTask().voiceSession != null) {
- // If this activity is being launched as part of a voice session, we need
- // to ensure that it is safe to do so. If the upcoming activity will also
- // be part of the voice session, we can only launch it if it has explicitly
- // said it supports the VOICE category, or it is a part of the calling App.
- if ((launchFlags & FLAG_ACTIVITY_NEW_TASK) == 0
- && sourceRecord.info.applicationInfo.uid != aInfo.applicationInfo.uid) {
- try {
- intent.addCategory(Intent.CATEGORY_VOICE);
- if (!AppGlobals.getPackageManager().activitySupportsIntent(
- intent.getComponent(), intent, resolvedType)) {
- Slog.w(TAG,
- "Activity being started in current voice task does not support voice:"
- + intent);
- err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
- }
- } catch (RemoteException e) {
- Slog.w(TAG, "Failure checking voice capabilities", e);
- err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
- }
- }
- }
- if (err == ActivityManager.START_SUCCESS && voiceSession != null) {
- // If the caller is starting a new voice session, just make sure the target
- // is actually allowing it to run this way.
- try {
- if (!AppGlobals.getPackageManager().activitySupportsIntent(intent.getComponent(),
- intent, resolvedType)) {
- Slog.w(TAG,
- "Activity being started in new voice task does not support:"
- + intent);
- err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
- }
- } catch (RemoteException e) {
- Slog.w(TAG, "Failure checking voice capabilities", e);
- err = ActivityManager.START_NOT_VOICE_COMPATIBLE;
- }
- }
- final ActivityStack resultStack = resultRecord == null ? null : resultRecord.getStack();
- if (err != START_SUCCESS) {
- if (resultRecord != null) {
- resultStack.sendActivityResultLocked(
- -1, resultRecord, resultWho, requestCode, RESULT_CANCELED, null);
- }
- ActivityOptions.abort(options);
- return err;
- }
- // 校验当前应用是否有开启权限, 我们的普通开启肯定是有权限啦, 所以可以忽略啦
- boolean abort = !mSupervisor.checkStartAnyActivityPermission(intent, aInfo, resultWho,
- requestCode, callingPid, callingUid, callingPackage, ignoreTargetSecurity, callerApp,
- resultRecord, resultStack, options);
- abort |= !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
- callingPid, resolvedType, aInfo.applicationInfo);
- if (mService.mController != null) {
- try {
- // The Intent we give to the watcher has the extra data
- // stripped off, since it can contain private information.
- Intent watchIntent = intent.cloneFilter();
- abort |= !mService.mController.activityStarting(watchIntent,
- aInfo.applicationInfo.packageName);
- } catch (RemoteException e) {
- mService.mController = null;
- }
- }
- mInterceptor.setStates(userId, realCallingPid, realCallingUid, startFlags, callingPackage);
- mInterceptor.intercept(intent, rInfo, aInfo, resolvedType, inTask, callingPid, callingUid,
- options);
- intent = mInterceptor.mIntent;
- rInfo = mInterceptor.mRInfo;
- aInfo = mInterceptor.mAInfo;
- resolvedType = mInterceptor.mResolvedType;
- inTask = mInterceptor.mInTask;
- callingPid = mInterceptor.mCallingPid;
- callingUid = mInterceptor.mCallingUid;
- options = mInterceptor.mActivityOptions;
- if (abort) {
- if (resultRecord != null) {
- resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
- RESULT_CANCELED, null);
- }
- // We pretend to the caller that it was really started, but
- // they will just get a cancel result.
- ActivityOptions.abort(options);
- return START_ABORTED;
- }
- // If permissions need a review before any of the App components can run, we
- // launch the review activity and pass a pending intent to start the activity
- // we are to launching now after the review is completed.
- if (mService.mPermissionReviewRequired && aInfo != null) {
- if (mService.getPackageManagerInternalLocked().isPermissionsReviewRequired(
- aInfo.packageName, userId)) {
- IIntentSender target = mService.getIntentSenderLocked(
- ActivityManager.INTENT_SENDER_ACTIVITY, callingPackage,
- callingUid, userId, null, null, 0, new Intent[]{intent},
- new String[]{resolvedType}, PendingIntent.FLAG_CANCEL_CURRENT
- | PendingIntent.FLAG_ONE_SHOT, null);
- final int flags = intent.getFlags();
- Intent newIntent = new Intent(Intent.ACTION_REVIEW_PERMISSIONS);
- newIntent.setFlags(flags
- | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
- newIntent.putExtra(Intent.EXTRA_PACKAGE_NAME, aInfo.packageName);
- newIntent.putExtra(Intent.EXTRA_INTENT, new IntentSender(target));
- if (resultRecord != null) {
- newIntent.putExtra(Intent.EXTRA_RESULT_NEEDED, true);
- }
- intent = newIntent;
- resolvedType = null;
- callingUid = realCallingUid;
- callingPid = realCallingPid;
- rInfo = mSupervisor.resolveIntent(intent, resolvedType, userId);
- aInfo = mSupervisor.resolveActivity(intent, rInfo, startFlags,
- null /*profilerInfo*/);
- if (DEBUG_PERMISSIONS_REVIEW) {
- Slog.i(TAG, "START u" + userId + "{" + intent.toShortString(true, true,
- true, false) + "} from uid" + callingUid + "on display"
- + (mSupervisor.mFocusedStack == null
- ? DEFAULT_DISPLAY : mSupervisor.mFocusedStack.mDisplayId));
- }
- }
- }
- // If we have an ephemeral App, abort the process of launching the resolved intent.
- // Instead, launch the ephemeral installer. Once the installer is finished, it
- // starts either the intent we resolved here [on install error] or the ephemeral
- // App [on install success].
- if (rInfo != null && rInfo.auxiliaryInfo != null) {
- intent = createLaunchIntent(rInfo.auxiliaryInfo, ephemeralIntent,
- callingPackage, verificationBundle, resolvedType, userId);
- resolvedType = null;
- callingUid = realCallingUid;
- callingPid = realCallingPid;
- aInfo = mSupervisor.resolveActivity(intent, rInfo, startFlags, null /*profilerInfo*/);
- }
- // 创建出我们的目标 ActivityRecord 对象, 存到传入数组 0 索引上
- ActivityRecord r = new ActivityRecord(mService, callerApp, callingPid, callingUid,
- callingPackage, intent, resolvedType, aInfo, mService.getGlobalConfiguration(),
- resultRecord, resultWho, requestCode, componentSpecified, voiceSession != null,
- mSupervisor, options, sourceRecord);
- if (outActivity != null) {
- outActivity[0] = r;
- }
- if (r.appTimeTracker == null && sourceRecord != null) {
- // If the caller didn't specify an explicit time tracker, we want to continue
- // tracking under any it has.
- r.appTimeTracker = sourceRecord.appTimeTracker;
- }
- final ActivityStack stack = mSupervisor.mFocusedStack;
- if (voiceSession == null && (stack.mResumedActivity == null
- || stack.mResumedActivity.info.applicationInfo.uid != callingUid)) {
- if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid,
- realCallingPid, realCallingUid, "Activity start")) {
- PendingActivityLaunch pal = new PendingActivityLaunch(r,
- sourceRecord, startFlags, stack, callerApp);
- mPendingActivityLaunches.add(pal);
- ActivityOptions.abort(options);
- return ActivityManager.START_SWITCHES_CANCELED;
- }
- }
- if (mService.mDidAppSwitch) {
- // This is the second allowed switch since we stopped switches,
- // so now just generally allow switches. Use case: user presses
- // home (switches disabled, switch to home, mDidAppSwitch now true);
- // user taps a home icon (coming from home so allowed, we hit here
- // and now allow anyone to switch again).
- mService.mAppSwitchesAllowedTime = 0;
- } else {
- mService.mDidAppSwitch = true;
- }
- // 检查是否有需要延迟启动的 Activity, 如果有, 则启动
- doPendingActivityLaunchesLocked(false);
- // 跳转到另外一个 startActivity 函数中继续执行
- return startActivity(r, sourceRecord, voiceSession, voiceInteractor, startFlags, true,
- options, inTask, outActivity);
- }
这个函数就分析完了, 写的很长, 但是逻辑并不复杂, 主要就是处理 startActivityForResult 的逻辑和校验权限, 最后还创建了一个目标 activity 的 ActivityRecord 对象就完了. 我们继续看下一个重载函数
- private int startActivity(final ActivityRecord r, ActivityRecord sourceRecord,
- IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
- int startFlags, boolean doResume, ActivityOptions options, TaskRecord inTask,
- ActivityRecord[] outActivity) {
- int result = START_CANCELED;
- try {
- // 阻塞一下 Surface
- mService.mWindowManager.deferSurfaceLayout();
- result = startActivityUnchecked(r, sourceRecord, voiceSession, voiceInteractor,
- startFlags, doResume, options, inTask, outActivity);
- } finally {
- // If we are not able to proceed, disassociate the activity from the task. Leaving an
- // activity in an incomplete state can lead to issues, such as performing operations
- // without a Windows container.
- if (!ActivityManager.isStartResultSuccessful(result)
- && mStartActivity.getTask() != null) {
- mStartActivity.getTask().removeActivity(mStartActivity);
- }
- mService.mWindowManager.continueSurfaceLayout();
- }
- postStartActivityProcessing(r, result, mSupervisor.getLastStack().mStackId, mSourceRecord,
- mTargetStack);
- return result;
- }
这个函数很简单, 也只是通知 Wms(WindowManagerService)阻塞一下 Surface, 然后就转发到了 startActivityUnchecked 函数中继续处理了, 我们接着看 startActivityUnchecked 的实现吧.
- private int startActivityUnchecked(final ActivityRecord r, ActivityRecord sourceRecord,
- IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
- int startFlags, boolean doResume, ActivityOptions options, TaskRecord inTask,
- ActivityRecord[] outActivity) {
- // 根据启动 intent 识别启动模式, 如果是 startActivityForResult 并且启动模式是 NEW_TASK 的话, 就会让 startActivityForResult 的回调失效
- setInitialState(r, options, inTask, doResume, startFlags, sourceRecord, voiceSession,
- voiceInteractor);
- // 判断启动模式, 并且在 mLaunchFlags 上追加对应的标记
- computeLaunchingTaskFlags();
- // 获取到 Activity 的启动栈
- computeSourceStack();
- // 根据上面的计算, 应用识别到的 flags
- mIntent.setFlags(mLaunchFlags);
- ActivityRecord reusedActivity = getReusableIntentActivity();
- final int preferredLaunchStackId =
- (mOptions != null) ? mOptions.getLaunchStackId() : INVALID_STACK_ID;
- final int preferredLaunchDisplayId =
- (mOptions != null) ? mOptions.getLaunchDisplayId() : DEFAULT_DISPLAY;
- if (reusedActivity != null) {
- // When the flags NEW_TASK and CLEAR_TASK are set, then the task gets reused but
- // still needs to be a lock task mode violation since the task gets cleared out and
- // the device would otherwise leave the locked task.
- if (mSupervisor.isLockTaskModeViolation(reusedActivity.getTask(),
- (mLaunchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
- == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))) {
- mSupervisor.showLockTaskToast();
- Slog.e(TAG, "startActivityUnchecked: Attempt to violate Lock Task Mode");
- return START_RETURN_LOCK_TASK_MODE_VIOLATION;
- }
- if (mStartActivity.getTask() == null) {
- mStartActivity.setTask(reusedActivity.getTask());
- }
- if (reusedActivity.getTask().intent == null) {
- // This task was started because of movement of the activity based on affinity...
- // Now that we are actually launching it, we can assign the base intent.
- reusedActivity.getTask().setIntent(mStartActivity);
- }
- // This code path leads to delivering a new intent, we want to make sure we schedule it
- // as the first operation, in case the activity will be resumed as a result of later
- // operations.
- if ((mLaunchFlags & FLAG_ACTIVITY_CLEAR_TOP) != 0
- || isDocumentLaunchesIntoExisting(mLaunchFlags)
- || mLaunchSingleInstance || mLaunchSingleTask) {
- final TaskRecord task = reusedActivity.getTask();
- // In this situation we want to remove all activities from the task up to the one
- // being started. In most cases this means we are resetting the task to its initial
- // state.
- final ActivityRecord top = task.performClearTaskForReuseLocked(mStartActivity,
- mLaunchFlags);
- // The above code can remove {@code reusedActivity} from the task, leading to the
- // the {@code ActivityRecord} removing its reference to the {@code TaskRecord}. The
- // task reference is needed in the call below to
- // {@link setTargetStackAndMoveToFrontIfNeeded}.
- if (reusedActivity.getTask() == null) {
- reusedActivity.setTask(task);
- }
- if (top != null) {
- if (top.frontOfTask) {
- // Activity aliases may mean we use different intents for the top activity,
- // so make sure the task now has the identity of the new intent.
- top.getTask().setIntent(mStartActivity);
- }
- deliverNewIntent(top);
- }
- }
- sendPowerHintForLaunchStartIfNeeded(false /* forceSend */, reusedActivity);
- reusedActivity = setTargetStackAndMoveToFrontIfNeeded(reusedActivity);
- final ActivityRecord outResult =
- outActivity != null && outActivity.length> 0 ? outActivity[0] : null;
- // When there is a reused activity and the current result is a trampoline activity,
- // set the reused activity as the result.
- if (outResult != null && (outResult.finishing || outResult.noDisplay)) {
- outActivity[0] = reusedActivity;
- }
- if ((mStartFlags & START_FLAG_ONLY_IF_NEEDED) != 0) {
- // We don't need to start a new activity, and the client said not to do anything
- // if that is the case, so this is it! And for paranoia, make sure we have
- // correctly resumed the top activity.
- resumeTargetStackIfNeeded();
- return START_RETURN_INTENT_TO_CALLER;
- }
- setTaskFromIntentActivity(reusedActivity);
- if (!mAddingToTask && mReuseTask == null) {
- // We didn't do anything... but it was needed (a.k.a., client don't use that
- // intent!) And for paranoia, make sure we have correctly resumed the top activity.
- resumeTargetStackIfNeeded();
- if (outActivity != null && outActivity.length> 0) {
- outActivity[0] = reusedActivity;
- }
- return START_TASK_TO_FRONT;
- }
- }
- // 校验目标页面的包名, 很有可能是没有安装该应用
- if (mStartActivity.packageName == null) {
- final ActivityStack sourceStack = mStartActivity.resultTo != null
- ? mStartActivity.resultTo.getStack() : null;
- if (sourceStack != null) {
- sourceStack.sendActivityResultLocked(-1 /* callingUid */, mStartActivity.resultTo,
- mStartActivity.resultWho, mStartActivity.requestCode, RESULT_CANCELED,
- null /* data */);
- }
- ActivityOptions.abort(mOptions);
- return START_CLASS_NOT_FOUND;
- }
- // If the activity being launched is the same as the one currently at the top, then
- // we need to check if it should only be launched once.
- final ActivityStack topStack = mSupervisor.mFocusedStack;
- final ActivityRecord topFocused = topStack.topActivity();
- final ActivityRecord top = topStack.topRunningNonDelayedActivityLocked(mNotTop);
- final boolean dontStart = top != null && mStartActivity.resultTo == null
- && top.realActivity.equals(mStartActivity.realActivity)
- && top.userId == mStartActivity.userId
- && top.App != null && top.App.thread != null
- && ((mLaunchFlags & FLAG_ACTIVITY_SINGLE_TOP) != 0
- || mLaunchSingleTop || mLaunchSingleTask);
- if (dontStart) {
- // For paranoia, make sure we have correctly resumed the top activity.
- topStack.mLastPausedActivity = null;
- if (mDoResume) {
- mSupervisor.resumeFocusedStackTopActivityLocked();
- }
- ActivityOptions.abort(mOptions);
- if ((mStartFlags & START_FLAG_ONLY_IF_NEEDED) != 0) {
- // We don't need to start a new activity, and the client said not to do
- // anything if that is the case, so this is it!
- return START_RETURN_INTENT_TO_CALLER;
- }
- deliverNewIntent(top);
- // Don't use mStartActivity.task to show the toast. We're not starting a new activity
- // but reusing 'top'. Fields in mStartActivity may not be fully initialized.
- mSupervisor.handleNonResizableTaskIfNeeded(top.getTask(), preferredLaunchStackId,
- preferredLaunchDisplayId, topStack.mStackId);
- return START_DELIVERED_TO_TOP;
- }
- // 接下来判断是否需要新创建一个 task 出来, 比如 NEW_TASK
- // 如果不需要新建就直接把目标 Activity 放入到对应的 task 内, 在 setTaskFromSourceRecord 内执行
- boolean newTask = false;
- final TaskRecord taskToAffiliate = (mLaunchTaskBehind && mSourceRecord != null)
- ? mSourceRecord.getTask() : null;
- // Should this be considered a new task?
- int result = START_SUCCESS;
- if (mStartActivity.resultTo == null && mInTask == null && !mAddingToTask
- && (mLaunchFlags & FLAG_ACTIVITY_NEW_TASK) != 0) {
- newTask = true;
- result = setTaskFromReuseOrCreateNewTask(
- taskToAffiliate, preferredLaunchStackId, topStack);
- } else if (mSourceRecord != null) {
- result = setTaskFromSourceRecord();
- } else if (mInTask != null) {
- result = setTaskFromInTask();
- } else {
- // This not being started from an existing activity, and not part of a new task...
- // just put it in the top task, though these days this case should never happen.
- setTaskToCurrentTopOrCreateNewTask();
- }
- if (result != START_SUCCESS) {
- return result;
- }
- mService.grantUriPermissionFromIntentLocked(mCallingUid, mStartActivity.packageName,
- mIntent, mStartActivity.getUriPermissionsLocked(), mStartActivity.userId);
- mService.grantEphemeralAccessLocked(mStartActivity.userId, mIntent,
- mStartActivity.appInfo.uid, UserHandle.getAppId(mCallingUid));
- if (mSourceRecord != null) {
- mStartActivity.getTask().setTaskToReturnTo(mSourceRecord);
- }
- if (newTask) {
- EventLog.writeEvent(
- EventLogTags.AM_CREATE_TASK, mStartActivity.userId,
- mStartActivity.getTask().taskId);
- }
- ActivityStack.logStartActivity(
- EventLogTags.AM_CREATE_ACTIVITY, mStartActivity, mStartActivity.getTask());
- mTargetStack.mLastPausedActivity = null;
- // 判断是否需要发送警告(PowerManager 实现), 判断依据是 resumedActivity 被回收或者是进程被杀死或者是目标 Activity 的进程和 resumedActivity 的进程不一致的时候
- sendPowerHintForLaunchStartIfNeeded(false /* forceSend */, mStartActivity);
- // 处理新增 Task 和启动动画的问题
- mTargetStack.startActivityLocked(mStartActivity, topFocused, newTask, mKeepCurTransition,
- mOptions);
- // 处理完启动栈任务栈的问题后, 准备执行发起者的 Resume 状态了
- if (mDoResume) {
- final ActivityRecord topTaskActivity =
- mStartActivity.getTask().topRunningActivityLocked();
- if (!mTargetStack.isFocusable()
- || (topTaskActivity != null && topTaskActivity.mTaskOverlay
- && mStartActivity != topTaskActivity)) {
- // If the activity is not focusable, we can't resume it, but still would like to
- // make sure it becomes visible as it starts (this will also trigger entry
- // animation). An example of this are PIP activities.
- // Also, we don't want to resume activities in a task that currently has an overlay
- // as the starting activity just needs to be in the visible paused state until the
- // over is removed.
- mTargetStack.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
- // Go ahead and tell Windows manager to execute App transition for this activity
- // since the App transition will not be triggered through the resume channel.
- mWindowManager.executeAppTransition();
- } else {
- // If the target stack was not previously focusable (previous top running activity
- // on that stack was not visible) then any prior calls to move the stack to the
- // will not update the focused stack. If starting the new activity now allows the
- // task stack to be focusable, then ensure that we now update the focused stack
- // accordingly.
- if (mTargetStack.isFocusable() && !mSupervisor.isFocusedStack(mTargetStack)) {
- mTargetStack.moveToFront("startActivityUnchecked");
- }
- // 从这开始 resume 我们的 MainActivity
- mSupervisor.resumeFocusedStackTopActivityLocked(mTargetStack, mStartActivity,
- mOptions);
- }
- } else {
- mTargetStack.addRecentActivityLocked(mStartActivity);
- }
- mSupervisor.updateUserStackLocked(mStartActivity.userId, mTargetStack);
- mSupervisor.handleNonResizableTaskIfNeeded(mStartActivity.getTask(), preferredLaunchStackId,
- preferredLaunchDisplayId, mTargetStack.mStackId);
- return START_SUCCESS;
- }
这个 startActivityUnchecked 做的核心功能就是识别并且处理启动模式, 以及关联的启动栈, 任务栈, 最后选择完合适的任务栈后就准备调用 resumeFocusedStackTopActivityLocked 继续开启 Activity, 我们来继续往下看.
- boolean resumeFocusedStackTopActivityLocked(
- ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
- if (!readyToResume()) {
- return false;
- }
- if (targetStack != null && isFocusedStack(targetStack)) {
- return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
- }
- final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
- if (r == null || r.state != RESUMED) {
- mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
- } else if (r.state == RESUMED) {
- // Kick off any lingering App transitions form the MoveTaskToFront operation.
- mFocusedStack.executeAppTransition(targetOptions);
- }
- return false;
- }
这个 resumeFocusedStackTopActivityLocked 函数定义在 ActivityStackSupervisor 类中, 从类名上可以看得出来这个是个管理者, 不参与具体实现, 所以函数也很短, 只是做简单的校验就立马转发, 很符合企业里领导的身份. 我们看紧接着就被转发到了 ActivityStack 下的 resumeTopActivityUncheckedLocked 中了, 下面这个就是 ActivityStack 下的 resumeTopActivityUncheckedLocked 函数了
- boolean resumeTopActivityUncheckedLocked(ActivityRecord prev, ActivityOptions options) {
- if (mStackSupervisor.inResumeTopActivity) {
- // Don't even start recursing.
- return false;
- }
- boolean result = false;
- try {
- // Protect against recursion.
- mStackSupervisor.inResumeTopActivity = true;
- result = resumeTopActivityInnerLocked(prev, options);
- } finally {
- mStackSupervisor.inResumeTopActivity = false;
- }
- // When resuming the top activity, it may be necessary to pause the top activity (for
- // example, returning to the lock screen. We suppress the normal pause logic in
- // {@link #resumeTopActivityUncheckedLocked}, since the top activity is resumed at the end.
- // We call the {@link ActivityStackSupervisor#checkReadyForSleepLocked} again here to ensure
- // any necessary pause logic occurs. In the case where the Activity will be shown regardless
- // of the lock screen, the call to {@link ActivityStackSupervisor#checkReadyForSleepLocked}
- // is skipped.
- final ActivityRecord next = topRunningActivityLocked(true /* focusableOnly */);
- if (next == null || !next.canTurnScreenOn()) {
- checkReadyForSleep();
- }
- return result;
- }
可以看一下, 他也没做什么业务处理. 而是转交给了另外一个函数去处理, 我们继续看
- private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options) {
- if (!mService.mBooting && !mService.mBooted) {
- // Not ready yet!
- return false;
- }
- // Find the next top-most activity to resume in this stack that is not finishing and is
- // focusable. If it is not focusable, we will fall into the case below to resume the
- // 我们的 TestActivity 已经入栈了, 所以 next 就是我们的 TestActivity 了
- final ActivityRecord next = topRunningActivityLocked(true /* focusableOnly */);
- final boolean hasRunningActivity = next != null;
- // TODO: Maybe this entire condition can get removed?
- if (hasRunningActivity && getDisplay() == null) {
- return false;
- }
- mStackSupervisor.cancelInitializingActivities();
- // Remember how we'll process this pause/resume situation, and ensure
- // that the state is reset however we wind up proceeding.
- final boolean userLeaving = mStackSupervisor.mUserLeaving;
- mStackSupervisor.mUserLeaving = false;
- if (!hasRunningActivity) {
- // There are no activities left in the stack, let's look somewhere else.
- return resumeTopActivityInNextFocusableStack(prev, options, "noMoreActivities");
- }
- next.delayedResume = false;
- // 如果我们要跳转的页面已经是 resume 状态, 那么直接通知 Wms 执行 Activity 的过渡
- if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
- mStackSupervisor.allResumedActivitiesComplete()) {
- // Make sure we have executed any pending transitions, since there
- // should be nothing left to do at this point.
- executeAppTransition(options);
- if (DEBUG_STATES) Slog.d(TAG_STATES,
- "resumeTopActivityLocked: Top activity resumed" + next);
- if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
- return false;
- }
- final TaskRecord nextTask = next.getTask();
- final TaskRecord prevTask = prev != null ? prev.getTask() : null;
- if (prevTask != null && prevTask.getStack() == this &&
- prevTask.isOverHomeStack() && prev.finishing && prev.frontOfTask) {
- if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
- if (prevTask == nextTask) {
- prevTask.setFrontOfTask();
- } else if (prevTask != topTask()) {
- // This task is going away but it was supposed to return to the home stack.
- // Now the task above it has to return to the home task instead.
- final int taskNdx = mTaskHistory.indexOf(prevTask) + 1;
- mTaskHistory.get(taskNdx).setTaskToReturnTo(HOME_ACTIVITY_TYPE);
- } else if (!isOnHomeDisplay()) {
- return false;
- } else if (!isHomeStack()){
- if (DEBUG_STATES) Slog.d(TAG_STATES,
- "resumeTopActivityLocked: Launching home next");
- return isOnHomeDisplay() &&
- mStackSupervisor.resumeHomeStackTask(prev, "prevFinished");
- }
- }
- // 判断一下是否已经进入睡眠状态, 如果进入睡眠状态 Activity 也会进入 Pause 状态
- if (shouldSleepOrShutDownActivities()
- && mLastPausedActivity == next
- && mStackSupervisor.allPausedActivitiesComplete()) {
- // Make sure we have executed any pending transitions, since there
- // should be nothing left to do at this point.
- executeAppTransition(options);
- if (DEBUG_STATES) Slog.d(TAG_STATES,
- "resumeTopActivityLocked: Going to sleep and all paused");
- if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
- return false;
- }
- // Make sure that the user who owns this activity is started. If not,
- // we will just leave it as is because someone should be bringing
- // another user's activities to the top of the stack.
- if (!mService.mUserController.hasStartedUserState(next.userId)) {
- Slog.w(TAG, "Skipping resume of top activity" + next
- + ": user" + next.userId + "is stopped");
- if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
- return false;
- }
- // The activity may be waiting for stop, but that is no longer
- // appropriate for it.
- mStackSupervisor.mStoppingActivities.remove(next);
- mStackSupervisor.mGoingToSleepActivities.remove(next);
- next.sleeping = false;
- mStackSupervisor.mActivitiesWaitingForVisibleActivity.remove(next);
- if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resuming" + next);
- // If we are currently pausing an activity, then don't do anything until that is done.
- if (!mStackSupervisor.allPausedActivitiesComplete()) {
- if (DEBUG_SWITCH || DEBUG_PAUSE || DEBUG_STATES) Slog.v(TAG_PAUSE,
- "resumeTopActivityLocked: Skip resume: some activity pausing.");
- if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
- return false;
- }
- mStackSupervisor.setLaunchSource(next.info.applicationInfo.uid);
- boolean lastResumedCanPip = false;
- final ActivityStack lastFocusedStack = mStackSupervisor.getLastStack();
- if (lastFocusedStack != null && lastFocusedStack != this) {
- // So, why aren't we using prev here??? See the param comment on the method. prev doesn't
- // represent the last resumed activity. However, the last focus stack does if it isn't null.
- final ActivityRecord lastResumed = lastFocusedStack.mResumedActivity;
- lastResumedCanPip = lastResumed != null && lastResumed.checkEnterPictureInPictureState(
- "resumeTopActivity", userLeaving /* beforeStopping */);
- }
- // If the flag RESUME_WHILE_PAUSING is set, then continue to schedule the previous activity
- // to be paused, while at the same time resuming the new resume activity only if the
- // previous activity can't go into Pip since we want to give Pip activities a chance to
- // enter Pip before resuming the next activity.
- final boolean resumeWhilePausing = (next.info.flags & FLAG_RESUME_WHILE_PAUSING) != 0
- && !lastResumedCanPip;
- // 将发起者置入 Pause 状态, 也就是 MainActivity 改成 onPause 状态
- boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, next, false);
- if (mResumedActivity != null) {
- if (DEBUG_STATES) Slog.d(TAG_STATES,
- "resumeTopActivityLocked: Pausing" + mResumedActivity);
- pausing |= startPausingLocked(userLeaving, false, next, false);
- }
- if (pausing && !resumeWhilePausing) {
- if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG_STATES,
- "resumeTopActivityLocked: Skip resume: need to start pausing");
- // At this point we want to put the upcoming activity's process
- // at the top of the LRU list, since we know we will be needing it
- // very soon and it would be a waste to let it get killed if it
- // happens to be sitting towards the end.
- if (next.App != null && next.App.thread != null) {
- mService.updateLruProcessLocked(next.App, true, null);
- }
- if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
- return true;
- } else if (mResumedActivity == next && next.state == ActivityState.RESUMED &&
- mStackSupervisor.allResumedActivitiesComplete()) {
- // It is possible for the activity to be resumed when we paused back stacks above if the
- // next activity doesn't have to wait for pause to complete.
- // So, nothing else to-do except:
- // Make sure we have executed any pending transitions, since there
- // should be nothing left to do at this point.
- executeAppTransition(options);
- if (DEBUG_STATES) Slog.d(TAG_STATES,
- "resumeTopActivityLocked: Top activity resumed (dontWaitForPause)" + next);
- if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
- return true;
- }
- // If the most recent activity was noHistory but was only stopped rather
- // than stopped+finished because the device went to sleep, we need to make
- // sure to finish it as we're making a new activity topmost.
- if (shouldSleepActivities() && mLastNoHistoryActivity != null &&
- !mLastNoHistoryActivity.finishing) {
- if (DEBUG_STATES) Slog.d(TAG_STATES,
- "no-history finish of" + mLastNoHistoryActivity + "on new resume");
- requestFinishActivityLocked(mLastNoHistoryActivity.appToken, Activity.RESULT_CANCELED,
- null, "resume-no-history", false);
- mLastNoHistoryActivity = null;
- }
- if (prev != null && prev != next) {
- if (!mStackSupervisor.mActivitiesWaitingForVisibleActivity.contains(prev)
- && next != null && !next.nowVisible) {
- mStackSupervisor.mActivitiesWaitingForVisibleActivity.add(prev);
- if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
- "Resuming top, waiting visible to hide:" + prev);
- } else {
- // The next activity is already visible, so hide the previous
- // activity's Windows right now so we can show the new one ASAP.
- // We only do this if the previous is finishing, which should mean
- // it is on top of the one being resumed so hiding it quickly
- // is good. Otherwise, we want to do the normal route of allowing
- // the resumed activity to be shown so we can decide if the
- // previous should actually be hidden depending on whether the
- // new one is found to be full-screen or not.
- if (prev.finishing) {
- prev.setVisibility(false);
- if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
- "Not waiting for visible to hide:" + prev + ", waitingVisible="
- + mStackSupervisor.mActivitiesWaitingForVisibleActivity.contains(prev)
- + ", nowVisible=" + next.nowVisible);
- } else {
- if (DEBUG_SWITCH) Slog.v(TAG_SWITCH,
- "Previous already visible but still waiting to hide:" + prev
- + ", waitingVisible="
- + mStackSupervisor.mActivitiesWaitingForVisibleActivity.contains(prev)
- + ", nowVisible=" + next.nowVisible);
- }
- }
- }
- // Launching this App's activity, make sure the App is no longer
- // considered stopped.
- try {
- AppGlobals.getPackageManager().setPackageStoppedState(
- next.packageName, false, next.userId); /* TODO: Verify if correct userid */
- } catch (RemoteException e1) {
- } catch (IllegalArgumentException e) {
- Slog.w(TAG, "Failed trying to unstop package"
- + next.packageName + ":" + e);
- }
- // We are starting up the next activity, so tell the Windows manager
- // that the previous one will be hidden soon. This way it can know
- // to ignore it when computing the desired screen orientation.
- // 这里准备开启页面
- boolean anim = true;
- if (prev != null) {
- if (prev.finishing) {
- if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
- "Prepare close transition: prev=" + prev);
- if (mNoAnimActivities.contains(prev)) {
- anim = false;
- mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
- } else {
- mWindowManager.prepareAppTransition(prev.getTask() == next.getTask()
- ? TRANSIT_ACTIVITY_CLOSE
- : TRANSIT_TASK_CLOSE, false);
- }
- prev.setVisibility(false);
- } else {
- if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION,
- "Prepare open transition: prev=" + prev);
- if (mNoAnimActivities.contains(next)) {
- anim = false;
- mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
- } else {
- // 准备启动
- mWindowManager.prepareAppTransition(prev.getTask() == next.getTask()
- ? TRANSIT_ACTIVITY_OPEN
- : next.mLaunchTaskBehind
- ? TRANSIT_TASK_OPEN_BEHIND
- : TRANSIT_TASK_OPEN, false);
- }
- }
- } else {
- if (DEBUG_TRANSITION) Slog.v(TAG_TRANSITION, "Prepare open transition: no previous");
- if (mNoAnimActivities.contains(next)) {
- anim = false;
- mWindowManager.prepareAppTransition(TRANSIT_NONE, false);
- } else {
- mWindowManager.prepareAppTransition(TRANSIT_ACTIVITY_OPEN, false);
- }
- }
- Bundle resumeAnimOptions = null;
- if (anim) {
- ActivityOptions opts = next.getOptionsForTargetActivityLocked();
- if (opts != null) {
- resumeAnimOptions = opts.toBundle();
- }
- next.applyOptionsLocked();
- } else {
- next.clearOptionsLocked();
- }
- ActivityStack lastStack = mStackSupervisor.getLastStack();
- if (next.App != null && next.App.thread != null) {
- if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Resume running:" + next
- + "stopped=" + next.stopped + "visible=" + next.visible);
- // If the previous activity is translucent, force a visibility update of
- // the next activity, so that it's added to WM's opening App list, and
- // transition animation can be set up properly.
- // For example, pressing Home button with a translucent activity in focus.
- // Launcher is already visible in this case. If we don't add it to opening
- // apps, maybeUpdateTransitToWallpaper() will fail to identify this as a
- // TRANSIT_WALLPAPER_OPEN animation, and run some funny animation.
- final boolean lastActivityTranslucent = lastStack != null
- && (!lastStack.mFullscreen
- || (lastStack.mLastPausedActivity != null
- && !lastStack.mLastPausedActivity.fullscreen));
- // The contained logic must be synchronized, since we are both changing the visibility
- // and updating the {@link Configuration}. {@link ActivityRecord#setVisibility} will
- // ultimately cause the client code to schedule a layout. Since layouts retrieve the
- // current {@link Configuration}, we must ensure that the below code updates it before
- // the layout can occur.
- synchronized(mWindowManager.getWindowManagerLock()) {
- // This activity is now becoming visible.
- if (!next.visible || next.stopped || lastActivityTranslucent) {
- next.setVisibility(true);
- }
- // schedule launch ticks to collect information about slow apps.
- next.startLaunchTickingLocked();
- ActivityRecord lastResumedActivity =
- lastStack == null ? null :lastStack.mResumedActivity;
- ActivityState lastState = next.state;
- mService.updateCpuStats();
- if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to RESUMED:" + next
- + "(in existing)");
- setResumedActivityLocked(next, "resumeTopActivityInnerLocked");
- // 更新进程信息
- mService.updateLruProcessLocked(next.App, true, null);
- updateLRUListLocked(next);
- mService.updateOomAdjLocked();
- // Have the Windows manager re-evaluate the orientation of
- // the screen based on the new activity order.
- boolean notUpdated = true;
- if (mStackSupervisor.isFocusedStack(this)) {
- // We have special rotation behavior when Keyguard is locked. Make sure all
- // activity visibilities are set correctly as well as the transition is updated
- // if needed to get the correct rotation behavior.
- // TODO: Remove this once visibilities are set correctly immediately when
- // starting an activity.
- if (mStackSupervisor.mKeyguardController.isKeyguardLocked()) {
- mStackSupervisor.ensureActivitiesVisibleLocked(null /* starting */,
- 0 /* configChanges */, false /* preserveWindows */);
- }
- final Configuration config = mWindowManager.updateOrientationFromAppTokens(
- mStackSupervisor.getDisplayOverrideConfiguration(mDisplayId),
- next.mayFreezeScreenLocked(next.App) ? next.appToken : null,
- mDisplayId);
- if (config != null) {
- next.frozenBeforeDestroy = true;
- }
- notUpdated = !mService.updateDisplayOverrideConfigurationLocked(config, next,
- false /* deferResume */, mDisplayId);
- }
- if (notUpdated) {
- // The configuration update wasn't able to keep the existing
- // instance of the activity, and instead started a new one.
- // We should be all done, but let's just make sure our activity
- // is still at the top and schedule another run if something
- // weird happened.
- ActivityRecord nextNext = topRunningActivityLocked();
- if (DEBUG_SWITCH || DEBUG_STATES) Slog.i(TAG_STATES,
- "Activity config changed during resume:" + next
- + ", new next:" + nextNext);
- if (nextNext != next) {
- // Do over!
- mStackSupervisor.scheduleResumeTopActivities();
- }
- if (!next.visible || next.stopped) {
- next.setVisibility(true);
- }
- next.completeResumeLocked();
- if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
- return true;
- }
- try {
- // 判断是否需要触发 onActivityResult 函数
- ArrayList<ResultInfo> a = next.results;
- if (a != null) {
- final int N = a.size();
- if (!next.finishing && N> 0) {
- if (DEBUG_RESULTS) Slog.v(TAG_RESULTS,
- "Delivering results to" + next + ":" + a);
- next.App.thread.scheduleSendResult(next.appToken, a);
- }
- }
- // 是否需要触发 onNewIntent 函数
- if (next.newIntents != null) {
- next.App.thread.scheduleNewIntent(
- next.newIntents, next.appToken, false /* andPause */);
- }
- // Well the App will no longer be stopped.
- // Clear App token stopped state in Windows manager if needed.
- next.notifyAppResumed(next.stopped);
- EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.userId,
- System.identityHashCode(next), next.getTask().taskId,
- next.shortComponentName);
- next.sleeping = false;
- mService.showUnsupportedZoomDialogIfNeededLocked(next);
- mService.showAskCompatModeDialogLocked(next);
- next.App.pendingUiClean = true;
- next.App.forceProcessStateUpTo(mService.mTopProcessState);
- next.clearOptionsLocked();
- // 最后通过 ActivityThread 的 scheduleResumeActivity 函数进行启动
- next.App.thread.scheduleResumeActivity(next.appToken, next.App.repProcState,
- mService.isNextTransitionForward(), resumeAnimOptions);
- if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Resumed"
- + next);
- } catch (Exception e) {
- // Whoops, need to restart this activity!
- if (DEBUG_STATES) Slog.v(TAG_STATES, "Resume failed; resetting state to"
- + lastState + ":" + next);
- next.state = lastState;
- if (lastStack != null) {
- lastStack.mResumedActivity = lastResumedActivity;
- }
- Slog.i(TAG, "Restarting because process died:" + next);
- if (!next.hasBeenLaunched) {
- next.hasBeenLaunched = true;
- } else if (SHOW_APP_STARTING_PREVIEW && lastStack != null &&
- mStackSupervisor.isFrontStackOnDisplay(lastStack)) {
- next.showStartingWindow(null /* prev */, false /* newTask */,
- false /* taskSwitch */);
- }
- mStackSupervisor.startSpecificActivityLocked(next, true, false);
- if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
- return true;
- }
- }
- // From this point on, if something goes wrong there is no way
- // to recover the activity.
- try {
- next.completeResumeLocked();
- } catch (Exception e) {
- // If any exception gets thrown, toss away this
- // activity and try the next one.
- Slog.w(TAG, "Exception thrown during resume of" + next, e);
- requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
- "resume-exception", true);
- if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
- return true;
- }
- } else {
- // Whoops, need to restart this activity!
- if (!next.hasBeenLaunched) {
- next.hasBeenLaunched = true;
- } else {
- if (SHOW_APP_STARTING_PREVIEW) {
- next.showStartingWindow(null /* prev */, false /* newTask */,
- false /* taskSwich */);
- }
- if (DEBUG_SWITCH) Slog.v(TAG_SWITCH, "Restarting:" + next);
- }
- if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Restarting" + next);
- mStackSupervisor.startSpecificActivityLocked(next, true, true);
- }
- if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked();
- return true;
- }
这个函数就是对目标页的很多状态判断, 还有 pause 我们的启动者, 然后通知 AT(ActivityThread)准备启动, 我们继续查看 AT 内的启动逻辑
@Override public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident, ActivityInfo info, Configuration curConfig, Configuration overrideConfig, CompatibilityInfo compatInfo, String referrer, IVoiceInteractor voiceInteractor, int procState, Bundle state, PersistableBundle persistentState, List<ResultInfo> pendingResults, List<ReferrerIntent> pendingNewIntents, boolean notResumed, boolean isForward, ProfilerInfo profilerInfo) { updateProcessState(procState, false); ActivityClientRecord r = new ActivityClientRecord(); r.token = token; r.ident = ident; r.intent = intent; r.referrer = referrer; r.voiceInteractor = voiceInteractor; r.activityInfo = info; r.compatInfo = compatInfo; r.state = state; r.persistentState = persistentState; r.pendingResults = pendingResults; r.pendingIntents = pendingNewIntents; r.startsNotResumed = notResumed; r.isForward = isForward; r.profilerInfo = profilerInfo; r.overrideConfig = overrideConfig; updatePendingConfiguration(curConfig); sendMessage(H.LAUNCH_ACTIVITY, r); }
这个函数内创建了一个 ActivityClientRecord 对象, 然后通过 handler 发送命令 LAUNCH_ACTIVITY, 我们看一下命令所对应的函数是什么吧
case LAUNCH_ACTIVITY: { Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStart"); final ActivityClientRecord r = (ActivityClientRecord) msg.obj; r.packageInfo = getPackageInfoNoCheck( r.activityInfo.applicationInfo, r.compatInfo); handleLaunchActivity(r, null, "LAUNCH_ACTIVITY"); Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); }
这个 case 下调用了 handleLaunchActivity 函数, 我们继续跟进分析
private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent, String reason) { // If we are getting ready to gc after going to the background, well // we are back active so skip it. unscheduleGcIdler(); mSomeActivitiesChanged = true; if (r.profilerInfo != null) { mProfiler.setProfiler(r.profilerInfo); mProfiler.startProfiling(); } // Make sure we are running with the most recent config. handleConfigurationChanged(null, null); if (localLOGV) Slog.v( TAG, "Handling launch of" + r); // Initialize before creating the activity if (!ThreadedRenderer.sRendererDisabled) { GraphicsEnvironment.earlyInitEGL(); } WindowManagerGlobal.initialize(); // 通过反射创建 Activity 的对象 // 并且触发 attach,onCreate,onStart 生命周期方法 // 此时我们也会自己掉用 setContentView 来组装 DecorView Activity a = performLaunchActivity(r, customIntent); if (a != null) { r.createdConfig = new Configuration(mConfiguration); reportSizeConfigurations(r); Bundle oldState = r.state; // 执行 onResume 生命周期方法, 还会通知 ViewRootImpl 绘制页面 handleResumeActivity(r.token, false, r.isForward, !r.activity.mFinished && !r.startsNotResumed, r.lastProcessedSeq, reason); if (!r.activity.mFinished && r.startsNotResumed) { // The activity manager actually wants this one to start out paused, because it // needs to be visible but isn't in the foreground. We accomplish this by going // through the normal startup (because activities expect to go through onResume() // the first time they run, before their Windows is displayed), and then pausing it. // However, in this case we do -not- need to do the full pause cycle (of freezing // and such) because the activity manager assumes it can just retain the current // state it has. performPauseActivityIfNeeded(r, reason); // We need to keep around the original state, in case we need to be created again. // But we only do this for pre-Honeycomb apps, which always save their state when // pausing, so we can not have them save their state when restarting from a paused // state. For HC and later, we want to (and can) let the state be saved as the // normal part of stopping the activity. if (r.isPreHoneycomb()) { r.state = oldState; } } } else { // If there was an error, for any reason, tell the activity manager to stop us. try { ActivityManager.getService() .finishActivity(r.token, Activity.RESULT_CANCELED, null, Activity.DONT_FINISH_TASK_WITH_ACTIVITY); } catch (RemoteException ex) { throw ex.rethrowFromSystemServer(); } } }
到此 startActivity 的完整流程都全部呈现给大家了, 里边的校验是真的多. 涉及到的技术面也很广, 偶尔读读源码还是可以收获很多的, 上面的每一个判断, 都是一个知识点, 有些在开发中可以用到, 有些判断是给系统 App 留的.
来源: https://www.cnblogs.com/kezhuang/p/10574747.html