Activity的启动过程
首先先列出Activity启动的过程中调用的一些方法,因为activity启动涉及到各种方法调用,而且调用的路线也各不一样,这里只会对activity启动的一条线进行分析,有兴趣的同学可以读android的源码进行分析
- Activity
- startActivity
- startActivityForResult
- Instrumentation
- execStartActivity
- ActivityManagerProxy
- startActivity
- ActivityManagerService
- startActivity
- startActivityAsUser
- ActivityStarter
- startActivityMayWait
- startActivityLocked
- startActivityUnchecked
- postStartActivityUncheckedProcessing
- ActivityStack
- startActivityLocked
- ActivityStackSupervisor
- resumeFocusedStackTopActivityLocked
- ActivityStack
- resumeTopActivityUncheckedLocked
- resumeTopActivityInnerLocked
- ActivityThread
- scheduleLaunchActivity
- H(Handler) - LAUNCH_ACTIVITY
通过源码发现,startActivity进入到startActivityForResult的方法,然后进入到Instrumentation的execStartActivity方法。
在进入Instrumentation之前,先对Instrumentation做简短的介绍:
-
其包含有2个内部类:ActivityMoniter、ActivityResult
ActivityMoniter:有关特定的Intent的监视。一个ActivityMoniter类的实例通过函数addMonitor(Instrumentation.ActivityMonitor)添加到当前instrumentation中,一旦添加后,每当启动一个新的Activity,ActivityMoniter就会检测,如果匹配,其hit count计数更新等其他操作。 一个ActivityMonitor也可以用来寻找一个Activity,通过waitForActivity()方法,这个函数将返直到匹配的活动被创建。
ActivityResult:一个活动执行的结果说明,返回到原来的活动。 -
创建,暂停和恢复Activity的时候,都是通过调用Instrumentation的callActivityOnCreate,callActivityOnPause和callActivityOnResume等方法来实现对Activity方法生命周期调用
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 {
// Note we want to go through this call for compatibility with
// applications that may have overridden the method.
startActivityForResult(intent, -1);
}
}
public void startActivityForResult(@RequiresPermission Intent intent, int requestCode,
@Nullable Bundle options) {
if (mParent == null) {
options = transferSpringboardActivityOptions(options);
// 跳出Activity方法,进入到execStartActivity方法
Instrumentation.ActivityResult ar =
mInstrumentation.execStartActivity(
this, mMainThread.getApplicationThread(), mToken, this,
intent, requestCode, options);
if (ar != null) {
// 通知Activity启动的结果,这里会调用栈顶的Activity调用onPause方法
mMainThread.sendActivityResult(
mToken, mEmbeddedID, requestCode, ar.getResultCode(),
ar.getResultData());
}
if (requestCode >= 0) {
// If this start is requesting a result, we can avoid making
// the activity visible until the result is received. Setting
// this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
// activity hidden during this time, to avoid flickering.
// This can only be done when a result is requested because
// that guarantees we will get information back when the
// activity is finished, no matter what happens to it.
mStartedActivity = true;
}
cancelInputsAndStartExitTransition(options);
// TODO Consider clearing/flushing other event sources and events for child windows.
} else {
// 这里是低版本的系统启动Activity的方式,这里不进行分析
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);
}
}
}
我们进入到Instrumentation的execStartActivity方法,下面对execStartActivity的参数进行解析,
从源码可以看出,Instrumentation直接进入了ActivityManagerNative的startActivity方法,随后会调用checkStartActivityResult,这个方法会检查返回结果是否中Activity是否在AndroidManifest.xml中注册,如果没有就会抛出异常等。
public class Instrumentation {
...省略代码
public ActivityResult execStartActivity(...省略参数) {
...省略代码
// 进入到ActivityManagerProxy
int result = ActivityManagerNative.getDefault()
.startActivity(whoThread, who.getBasePackageName(), intent,
intent.resolveTypeIfNeeded(who.getContentResolver()),
token, target != null ? target.mEmbeddedID : null,
requestCode, 0, null, options);
// 检查startActivity返回结果是否成功
checkStartActivityResult(result, intents[0]);
...省略代码
return null;
}
public static void checkStartActivityResult(...省略参数) {
if (res >= ActivityManager.START_SUCCESS) {
return;
}
switch (res) {
...省略代码
// 此处是没有在AndroidManifest.xml里注册Activity时报的
case ActivityManager.START_INTENT_NOT_RESOLVED:
case ActivityManager.START_CLASS_NOT_FOUND:
if (intent instanceof Intent && ((Intent)intent).getComponent() != null)
throw new ActivityNotFoundException(
"Unable to find explicit activity class "
+ ((Intent)intent).getComponent().toShortString()
+ "; have you declared this activity in your AndroidManifest.xml?");
throw new ActivityNotFoundException(
"No Activity found to handle " + intent);
...省略代码
}
}
...省略代码
}
然后我们进入到ActivityManagerNative,下面对Activity的参数进行分析
- who:Activity的启动者
- contextThread:当前Activity的启动进程
- token:记录启动Activity的token值,有可能为空
- target:启动者,即启动新Activity的那个Activity
- intent:意图
- requestCode:启动Activity后,返回给当前Activity的值,通过onActivityResult返回
- options:附加参数
而ActivityMangagerNitive调用的是ActivityManagerProxy的startActivity方法,最后调用mRemote方法,mRemote是Binder类型,由此可见这里startActivity是通过调用远程ActivityManagerService的binder调用的是远程的onStransct方法,而ActivityManagerService是ActivityMangagerNitive的子类,onStransct的实现是在ActivityMangagerNitive,所以我们进入到ActivityMangagerNitive,从ActivityMangagerNitive的onTransact方法可以看出调用的是子类的startActivity方法,我们知道ActivityManagerNative的子类是ActivityManagerService。
这里涉级到一个技巧,我们每个Activity都必须到AndroidManifest.xml里面去注册否则我们就无法启动这个Activity,我们是否想到有没有可能不注册Activity就能启动这个Activity呢?答案是肯定的,我们可以在AndroidManifest.xml里面注册一个代理的Activity,只要在进入ActivityManagerNative方法后,我们把我们的启动Activity换成一个在AndroidManifest.xml中己经注册好的代理类Activity,这样就可以欺骗系统说,我己经注册了这个Activity了,你不需要抛出找不到Activity的异常。
public abstract class ActivityManagerNative extends Binder implements IActivityManager
// 这是远程ActivityManagerService的onTransact方法
@Override
public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
throws RemoteException {
switch (code) {
// 根据传过来的code类型,我们知道会调用到进入到下面的case去
case START_ACTIVITY_TRANSACTION:
{
....省略代码
// 调用startActivity方法,具体的实现在ActivityManagerService
int result = startActivity(app, callingPackage, intent, resolvedType,
resultTo, resultWho, requestCode, startFlags, profilerInfo, options);
...省略代码
return true;
}
}
class ActivityManagerProxy implements IActivityManager
...省略代码
public int startActivity(...省略参数) throws RemoteException {
...
// 调用远程的transact方法,带上START_ACTIVITY_TRANSACTION的code类型
// 并把所有的参数都带到远程去
mRemote.transact(START_ACTIVITY_TRANSACTION, data, reply, 0);
...
return result;
}
...省略代码
}
}
下面我们分析ActivityManagerService的startActivity方法,startActivity调用的是startActivityAsUsery方法,startActivity比startActivityAsUsery多出了UserId,有了这个UserId,就可以检查调用者的权限。而startActivityAsUser会跳到ActivityStarter的startActivityMayWait方法
public final class ActivityManagerService extends ActivityManagerNative
implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback {
...省略代码
@Override
public final int startActivity(...省略参数) {
return startActivityAsUser(caller, callingPackage, intent, resolvedType, resultTo, resultWho, requestCode, startFlags, profilerInfo, bOptions, UserHandle.getCallingUserId());
}
...省略代码
@Override
public final int startActivityAsUser(...省略参数) {
...省略代码
// 跳出ActivityManagerSerivce,进入到ActivityStarter的startActivityMayWait方法
return mActivityStarter.startActivityMayWait(caller, -1, callingPackage, intent, resolvedType, null, null, resultTo, resultWho, requestCode, startFlags,
profilerInfo, null, null, bOptions, false, userId, null, null);
}
}
下面我们进入到ActivityStarter类,因为ActivityStarter类涉及的方法太多,这里只是会分析几个重要的调用:
-
首先,我们进入到ActivityStarter的startActivityMayWait方法 此方法里面mSupervisor.resolveIntent(intent, resolvedType, userId)会进入到PackageManagerService的resolveIntent方法,此方法是开发者使用隐式调用时,会弹出选择窗让用户自己使择打开的应用
-
然后,我们进入到了ActivityStarter的startActivityLocked方法, 此方法大部份是对调用者的权限进行验证,看是否调用者是否有权限进行操作
-
然后进入到ActivityStarter的startActivityUnchecked方法,此方法主要是判断Activity是以什么方式启动,还有以什么方式入Activity栈, 例如computeLaunchingTaskFlags(); 判断Activity启动的以什么方式启动
-
然后我们进入到ActivityStack的startActivityLocked方法,这方法主要是设置Activity的准备工作己经完成
-
最后由postStartActivityUncheckedProcessing方法发出通知Activity的启动工作完成
class ActivityStarter {
final int startActivityMayWait(...省略参数) {
...省略代码
ResolveInfo rInfo = mSupervisor.resolveIntent(intent, resolvedType, userId);
...省略代码
// 进入到startActivityLocked方法,此方法主要是检查Activity启动的权限
int res = startActivityLocked(caller, intent, ephemeralIntent, resolvedType, aInfo, rInfo, voiceSession, voiceInteractor,
resultTo, resultWho, requestCode, callingPid,
callingUid, callingPackage, realCallingPid, realCallingUid, startFlags,
options, ignoreTargetSecurity, componentSpecified, outRecord, container,
inTask);
...省略代码
return res;
}
}
final int startActivityLocked(...省略参数) {
....省略代码
//
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_SUCCESS;
}
try {
err = startActivityUnchecked(r, sourceRecord, voiceSession, voiceInteractor, startFlags, true, options, inTask);
} finally {
mService.mWindowManager.continueSurfaceLayout();
}
postStartActivityUncheckedProcessing(r, err, stack.mStackId, mSourceRecord, mTargetStack);
return err;
}
private int startActivityUnchecked(final ActivityRecord r, ActivityRecord sourceRecord, sume, ActivityOptions options, TaskRecord inTask) {
setInitialState(r, options, inTask, doResume, startFlags, sourceRecord, voiceSession, voiceInteractor);
// 还是用来初始化启动标志位的
computeLaunchingTaskFlags();
computeSourceStack();
...省略代码
mTargetStack.startActivityLocked(mStartActivity, newTask, mKeepCurTransition, mOptions);
...省略代码
return START_SUCCESS;
}
void postStartActivityUncheckedProcessing(
ActivityRecord r, int result, int prevFocusedStackId, ActivityRecord sourceRecord,
ActivityStack targetStack) {
...省略代码
// We're waiting for an activity launch to finish, but that activity simply
// brought another activity to front. Let startActivityMayWait() know about
// this, so it waits for the new activity to become visible instead.
if (result == START_TASK_TO_FRONT && !mSupervisor.mWaitingActivityLaunched.isEmpty()) {
mSupervisor.reportTaskToFrontNoLaunch(mStartActivity);
}
...省略代码
}
...省略代码
}
final class ActivityStack {
...省略代码
final void startActivityLocked(ActivityRecord r, boolean newTask, boolean keepCurTransition,
ActivityOptions options) {
...省略代码
// Don't do a starting window for mLaunchTaskBehind. More importantly make sure we
// tell WindowManager that r is visible even though it is at the back of the stack.
mWindowManager.setAppVisibility(r.appToken, true);
...
}
...
}
下面我们进入到ActivityStackSupervisor源码,reportTaskToFrontNoLaunch的作用是如果change为true就通知通知ActivityStarter待启动Activity对应的Task移动到了前台
public final class ActivityStackSupervisor implements DisplayListener {
...省略代码
void reportTaskToFrontNoLaunch(ActivityRecord r) {
boolean changed = false;
for (int i = mWaitingActivityLaunched.size() - 1; i >= 0; i--) {
WaitResult w = mWaitingActivityLaunched.remove(i);
if (w.who == null) {
changed = true;
// Set result to START_TASK_TO_FRONT so that startActivityMayWait() knows that
// the starting activity ends up moving another activity to front, and it should
// wait for this new activity to become visible instead.
// Do not modify other fields.
w.result = START_TASK_TO_FRONT;
}
}
if (changed) {
//notifyAll通知ActivityStarter待启动Activity对应的Task移动到了前台
mService.notifyAll();
}
}
...省略代码
}
发送通知后,就会调用ActivityStackSupervisor的resumeFocusedStackTopActivityLocked方法,然后调ActivityStack的resumeTopActivityUncheckedLocked方法,再调用ActivityStack的resumeTopActivityInnerLocked,最后进入到ActivityThread的scheduleLaunchActivity方法
前面讲过可以启动一个没有在AndroidManifest.xml里面注册过的Activity,只需要用动态代理的方法,把H的handleMessage拦截,然后在这里把我们没有注册的Activity放回到intent里面就可以实现启动没有注册的Activity
public final class ActivityThread {
public static void main(String[] args) {
...省略代码
// 下面是启动UI主线程
Looper.prepareMainLooper();
ActivityThread thread = new ActivityThread();
thread.attach(false);
if (sMainThreadHandler == null) {
sMainThreadHandler = thread.getHandler();
}
if (false) {
Looper.myLooper().setMessageLogging(new
LogPrinter(Log.DEBUG, "ActivityThread"));
}
// End of event ActivityThreadMain.
Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Looper.loop();
throw new RuntimeException("Main thread loop unexpectedly exited");
}
@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) {
...
// 发送启动Activity的消息
sendMessage(H.LAUNCH_ACTIVITY, r);
}
private class H extends Handler {
...省略代码
public void handleMessage(Message msg) {
if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + codeToString(msg.what));
switch (msg.what) {
case LAUNCH_ACTIVITY: {
Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStart");
final ActivityClientRecord r = (ActivityClientRecord) msg.obj;、
// 获取Activity信息
r.packageInfo = getPackageInfoNoCheck(
r.activityInfo.applicationInfo, r.compatInfo);
handleLaunchActivity(r, null, "LAUNCH_ACTIVITY");
Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
break;
...
}
}
...省略代码
}
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);
// 初始化WindowManagerService服务
WindowManagerGlobal.initialize();
// 创建Activity,这里会执行Activity的生命周期
Activity a = performLaunchActivity(r, customIntent);
// 如果Activity不为空,则启动Activity
if (a != null) {
r.createdConfig = new Configuration(mConfiguration);
reportSizeConfigurations(r);
Bundle oldState = r.state;
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 window 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);
...
}
} else {
// 找不到需要启动的Activity报错
// If there was an error, for any reason, tell the activity manager to stop us.
try {
ActivityManagerNative.getDefault()
.finishActivity(r.token, Activity.RESULT_CANCELED, null,
Activity.DONT_FINISH_TASK_WITH_ACTIVITY);
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
}
}
这里只对Activity的启动流程分析,没有对每个方法进行详情的分析,有兴趣的读者可以看Android的源码
网友评论