android的activity有多重的启动途径,这里我们将从桌面点击应用图标时的启动过程。
1、packageManager查询AndroidManifest.xml.,过滤以下intent过滤条件的Activity
<intent-filter>
<action android:name="android.intent.action.Main"/>
<category android:name="android.intent.category.LAUNCHEr">
android桌面的代码位于packges/apps/Launcher2,Launcher捕获用户在home页的点击事件并调用startActivitySafely方法。
进而调用startActivity。startActivity位于android的frameWork的activity中,其实该方法只是对startActivtyForResult的封装。下面给出startActivityForResult的代码。
public void More ...startActivityForResult(Intent intent, int requestCode, Bundle options) {
3423 if (mParent == null) {//非子activty,本例情形
3424 Instrumentation.ActivityResult ar =
3425 mInstrumentation.execStartActivity(
3426 this, mMainThread.getApplicationThread(), mToken, this,
3427 intent, requestCode, options);
3428 if (ar != null) {
3429 mMainThread.sendActivityResult(
3430 mToken, mEmbeddedID, requestCode, ar.getResultCode(),
3431 ar.getResultData());
3432 }
3433 if (requestCode >= 0) {//本例中requstCode = -1
3434 // If this start is requesting a result, we can avoid making
3435 // the activity visible until the result is received. Setting
3436 // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
3437 // activity hidden during this time, to avoid flickering.
3438 // This can only be done when a result is requested because
3439 // that guarantees we will get information back when the
3440 // activity is finished, no matter what happens to it.
3441 mStartedActivity = true;
3442 }
3443
3444 final View decor = mWindow != null ? mWindow.peekDecorView() : null;
3445 if (decor != null) {
3446 decor.cancelPendingInputEvents();
3447 }
3448 // TODO Consider clearing/flushing other event sources and events for child windows.
3449 } else {
3450 if (options != null) {
3451 mParent.startActivityFromChild(this, intent, requestCode, options);
3452 } else {
3453 // Note we want to go through this method for compatibility with
3454 // existing applications that may have overridden it.
3455 mParent.startActivityFromChild(this, intent, requestCode);
3456 }
3457 }
3458 }
这里着重要看
execStartActivity(this, mMainThread.getApplicationThread(), mToken, this,intent, requestCode, options)
这里实现了跨进程的Activity调度。该方法位于framworks/base/core/java/android/app/Instrumentation.java,此方法通过ActivityManager的startActivity唤起新的Activity,下面给出代码:
public ActivityResult More ...execStartActivity(
1398 Context who, IBinder contextThread, IBinder token, Activity target,
1399 Intent intent, int requestCode, Bundle options) {
1400 IApplicationThread whoThread = (IApplicationThread) contextThread;
1401 if (mActivityMonitors != null) {//
1402 synchronized (mSync) {
1403 final int N = mActivityMonitors.size();
1404 for (int i=0; i<N; i++) {
1405 final ActivityMonitor am = mActivityMonitors.get(i);
1406 if (am.match(who, null, intent)) {
1407 am.mHits++;
1408 if (am.isBlocking()) {
1409 return requestCode >= 0 ? am.getResult() : null;
1410 }
1411 break;
1412 }
1413 }
1414 }
1415 }
1416 try {
1417 intent.migrateExtraStreamToClipData();
1418 intent.prepareToLeaveProcess();
1419 int result = ActivityManagerNative.getDefault()//调用acitvityManger的startActivy方法,
1420 .startActivity(whoThread, who.getBasePackageName(), intent,
1421 intent.resolveTypeIfNeeded(who.getContentResolver()),
1422 token, target != null ? target.mEmbeddedID : null,
1423 requestCode, 0, null, null, options);
1424 checkStartActivityResult(result, intent);
1425 } catch (RemoteException e) {
1426 }
1427 return null;
1428 }
跟踪程序得到activty oncreate方法的调用栈。
对以上过程进行总结:
1、Launcher线程捕获onclick的点击事件,调用Launcher.startActivitySafely,进一步调用Launcher.startActivity,最后调用父类Activity的startActivity.
2、Activity和ActivityManagerService交互,引入Instrumention,将启动请求交给Instrumention,调用Instrumention.execStartActivity.
3、得到ActivityManagerService,调用其startActivity,这里做了进程切换(具体过程请查看源码)。
4、开启Activity,调用onCreate方法。