首页 » Android » Android 插件化开发——应用的启动过程源码解析(简单清晰)

Android 插件化开发——应用的启动过程源码解析(简单清晰)

原文 http://blog.csdn.net/csdn_aiyang/article/details/79208462

2018-01-31 02:01:15阅读(339)

首先,Android系统中的应用程序是由Launcher启动的,其实Launcher本身也是一个Activity。下面从Launcher开始启动App流程源码开始分析。

Launcher源码路径是:\packages\apps\Launcher2\src\com\android\launcher2\Launcher.java。

com.android.launcher.Launcher 就是我们的Launcher页面了。

public final class Launcher extends Activity implements View.OnClickListener, OnLongClickListener {
    // ...
}
既然是Activity,自然也有onCreate、onResume等生命周期了,按照逻辑,应该会去加载所有App信息,以网格的布局显示在页面上,果然,在 onResume 看到了这个方法 startLoaders() 。
@Override
protected void onResume() {
    super.onResume();
    if (mRestoring) {
        startLoaders();
    }
    // ...
}
这个方法就是用来加载所有App信息的,进入这个方法:
private void startLoaders() {
    boolean loadApplications = sModel.loadApplications(true, this, mLocaleChanged);
    sModel.loadUserItems(!mLocaleChanged, this, mLocaleChanged, loadApplications);
    mRestoring = false;
}
调用sModel(LauncherModel)的loadUserItems方法加载数据,sModel明显属于Model层,进入loadUserItems方法:

void loadUserItems(boolean isLaunching, Launcher launcher, boolean localeChanged,
            boolean loadApplications) {
    // ...
        mDesktopItemsLoaded = false;
        mDesktopItemsLoader = new DesktopItemsLoader(launcher, localeChanged, loadApplications,
                isLaunching);
        mDesktopLoaderThread = new Thread(mDesktopItemsLoader, "Desktop Items Loader");
        mDesktopLoaderThread.start();
    // ...
}
可以看见,使用 DesktopItemsLoader 在 mDesktopLoaderThread 线程中加载:
private class DesktopItemsLoader implements Runnable {
    // ...
    public void run() {
        // ...
        final Cursor c = contentResolver.query(LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
        // ...
        final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
        final int intentIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT);
        final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
        final int iconTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_TYPE);
        final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
        final int iconPackageIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_PACKAGE);
        final int iconResourceIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_RESOURCE);
        final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
        final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
        final int appWidgetIdIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.APPWIDGET_ID);
        final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
        final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
        final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
        final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
        final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
        final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
        final int displayModeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.DISPLAY_MODE);
        // ...
        // 通过launcher回调返回数据
        launcher.onDesktopItemsLoaded(uiDesktopItems, uiDesktopWidgets);
        // ...
    }
    // ...
}
然后,我们看Launcher的回调 onDesktopItemsLoaded 方法 及 递进的两个方法:
void onDesktopItemsLoaded(ArrayList<ItemInfo> shortcuts, ArrayList<LauncherAppWidgetInfo> appWidgets) {
    // ...
    bindDesktopItems(shortcuts, appWidgets);
}
private void bindDesktopItems(ArrayList<ItemInfo> shortcuts, ArrayList<LauncherAppWidgetInfo> appWidgets) {
    // ...
    mBinder = new DesktopBinder(this, shortcuts, appWidgets, drawerAdapter);
    mBinder.startBindingItems();
}
public void startBindingItems() {
    // ...
    obtainMessage(MESSAGE_BIND_ITEMS, 0, mShortcuts.size()).sendToTarget();
}
这里 obtainMessage() 方法其实是在使用Handler发送消息,直接看Handler的handleMessage方法:
@Override
public void handleMessage(Message msg) {
    // ...
    switch (msg.what) {
        // ...
        case MESSAGE_BIND_ITEMS: {
            launcher.bindItems(this, mShortcuts, msg.arg1, msg.arg2);
            break;
        }
        // ...
    }
}
接收到消息之后调用bindItems方法:
private void bindItems(Launcher.DesktopBinder binder, ArrayList<ItemInfo> shortcuts, int start, int count) {
    // ...
    case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
    case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
        final View shortcut = createShortcut((ApplicationInfo) item);
        workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1, !desktopLocked);
    // ...
}
这里我们只考虑app或者app快捷方式的情况,文件夹和widgets暂时不考虑。app或者app快捷方式实质上都是进入了这个逻辑中,调用createShortcut方法:
// 重载方法,最终都会调用这个方法
View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
    // ...
    TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
    // ...
    favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
    favorite.setText(info.title);
    favorite.setTag(info);
    favorite.setOnClickListener(this);
    // ...
}
这里首先 inflater 出 item 的布局,然后设置 text 和 OnClickListener ,还有tag,这个tag是 ApplicationInfo,里面包含了各种App信息,是从App的AndroidManifest.xml的<application>标签中解析出来的。既然设置了点击事件,显然,点击后应该会打开对应的App才对。所以继续看onClick方法:
public void onClick(View v) {
    Object tag = v.getTag();
    if (tag instanceof ApplicationInfo) {
        // Open shortcut
        final Intent intent = ((ApplicationInfo) tag).intent;
        startActivitySafely(intent);
    } else if (tag instanceof FolderInfo) {
        handleFolderClick((FolderInfo) tag);
    }
}
点击App就会通过startActivitySafely方法使用设置的 tag(ApplicationInfo 中的 intent), 进行跳转:
void startActivitySafely(Intent intent) {
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    // ...
    startActivity(intent);
    // ...
}
然后我们来看看打开某个app的时候整个流程是怎么走的。接着上面的的startActivity()方法走:
public void startActivity(Intent intent, @Nullable Bundle options) {
    // ...
    if (options != null) {
        startActivityForResult(intent, -1, options);
    } else {
        startActivityForResult(intent, -1);
    }
    // ...
}
可以看到,不管你是调用了startActivity还是startActivityForResult方法,startActivityForResult方法,并且如果是调用的startActivity,则默认requestCode就是-1,所以如果你想调用startActivityForResult的时候,注意不能把requestCode设置为-1,否则它的效果就跟startActivity一样了,不会再回调onActivityResult!,再看看startActivityForResult的实现:
public void startActivityForResult(Intent intent, int requestCode, @Nullable Bundle options) {
    Instrumentation.ActivityResult ar = mInstrumentation.execStartActivity(
            this, mMainThread.getApplicationThread(), mToken, this,
            intent, requestCode, options);
    // ...
}
可以看到,Activity内部是使用mInstrumentation(Instrumentation类型)执行execStartActivity方法来实现Activity跳转的,执行完毕后会返回一个Instrumentation.ActivityResult。然后查看Instrumentation::execStartActivity:

public ActivityResult execStartActivity(
    // ...
    int result = ActivityManagerNative.getDefault()
        .startActivity(whoThread, who.getBasePackageName(), intent,
                intent.resolveTypeIfNeeded(who.getContentResolver()),
                token, target != null ? target.mEmbeddedID : null,
                requestCode, 0, null, options);
    // ...
}
首先通过ActivityManagerNative.getDefault()获得一个IActivityManager的实现类:
static public IActivityManager getDefault() {
    return gDefault.get();
}
private static final Singleton<IActivityManager> gDefault = new Singleton<IActivityManager>() {
    protected IActivityManager create() {
        // 通过Binder IPC获取ActivityManager(IBinder)
        IBinder b = ServiceManager.getService("activity");
        // ...
        IActivityManager am = asInterface(b);
        // ...
        return am;
    }
};
static public IActivityManager asInterface(IBinder obj) {
    if (obj == null) {
        return null;
    }
    IActivityManager in =
        (IActivityManager)obj.queryLocalInterface(descriptor);
    if (in != null) {
        return in;
    }
    return new ActivityManagerProxy(obj);
}
先通过Binder IPC的方式从服务端获取一个Activity Manager,然后通过ActivityManagernative封装成一个代理ActivityManagerProxy对象,然后调用startActivity也是使用了Binder IPC进行与服务器端的通信,(整个Android系统的通信机制使用了大量的Binder IPC,这个以后再专门讨论这个吧),接着,我们进入到了com.android.server.am.ActivityManagerService的startActivity方法:
@Override
public final int startActivity(IApplicationThread caller, String callingPackage, Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode, int startFlags, ProfilerInfo profilerInfo, Bundle options) {
    return startActivityAsUser(caller, callingPackage, intent, resolvedType, resultTo, resultWho, requestCode, startFlags, profilerInfo, options, UserHandle.getCallingUserId());
}
接下来的调用链:

-> startActivityAsUser
-> startActivityMayWait
-> startActivityLocked
-> startActivityUncheckedLocked
-> targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options)
-> mStackSupervisor.resumeTopActivitiesLocked(this, r, options)
-> resumeTopActivityInnerLocked(prev, options);
-> mStackSupervisor.startSpecificActivityLocked(next, true, true)
我们看一下,最后这个 startSpecificActivityLocked 方法:

void startSpecificActivityLocked(ActivityRecord r, boolean andResume, boolean checkConfig) {
    ProcessRecord app = mService.getProcessRecordLocked(r.processName,
            r.info.applicationInfo.uid, true);
    // ...
    if (app != null && app.thread != null) {
        // ...
        realStartActivityLocked(r, app, andResume, checkConfig);
        return;
    }
    // ...mService
    mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0, "activity", r.intent.getComponent(), false, false, true);
}
首先从 mService 找出对应需要启动Activity的进程(通过进程名字和uid,进程名字可以在AndroidManifest.xml中配置)如果可以获取到,说明这个Activity所属的进程已经存在了,也就是说app已经在运行了,那就会调用realStartActivityLocked,否则,如果该Activity所在的App是第一次启动,则会调用mService.startProcessLocked方法:
final ProcessRecord startProcessLocked(/*...*/){
    // ...
    startProcessLocked(app, hostingType, hostingNameStr, abiOverride, entryPoint, entryPointArgs);
    // ...
}
final ProcessRecord startProcessLocked(String processName, ApplicationInfo info, boolean knownToBeDead, int intentFlags, String hostingType, ComponentName hostingName, boolean allowWhileBooting, boolean isolated, int isolatedUid, boolean keepIfLarge, String abiOverride, String entryPoint, String[] entryPointArgs, Runnable crashHandler) {
    // ...
    app = getProcessRecordLocked(processName, info.uid, keepIfLarge);
    // ...
    if (app == null) {
        app = newProcessRecordLocked(info, processName, isolated, isolatedUid);
    }
    // ...
}
private final void startProcessLocked(ProcessRecord app, String hostingType, String hostingNameStr, String abiOverride, String entryPoint, String[] entryPointArgs) {
    // ...
    // entryPoint表示一个类,它用来作为新创建的进程的主入口,会调用这个类的静态main方法,这个参数在startProcessLocked方法中会被检查重置,如果是null的话,就默认是android.app.ActivityThread。
    if (entryPoint == null) entryPoint = "android.app.ActivityThread";
    // ...
    Process.ProcessStartResult startResult = Process.start(entryPoint,
            app.processName, uid, uid, gids, debugFlags, mountExternal,
            app.info.targetSdkVersion, app.info.seinfo, requiredAbi, 
            instructionSet, app.info.dataDir, entryPointArgs);
    // ...  
}
这3个重载方法做的事情就是,先根据进程名字调用getProcessRecordLocked()获取ProcessRecord,如果ProcessRecord不存在,则调用newProcessRecordLocked()方法建立一个ProcessRecord,并且新的ProcessRecord绑定了ApplicationInfo,uid等信息,但后进入第三个重载方法,执行新建、启动进程。
再看Process::start的实现:
public static final ProcessStartResult start(/*...*/){
    // ...
    return startViaZygote(processClass, niceName, uid, gid, gids,
              debugFlags, mountExternal, targetSdkVersion, seInfo,
              abi, instructionSet, appDataDir, zygoteArgs);
    // ...
}
接下来就是通过Zygote进程fork一个新的进程作为app的进程。这里要需要讲的一个参数是processClass,这个参数表示一个类,它用来作为新创建的进程的主入口,会调用这个类的静态main方法,这个参数在startProcessLocked方法中会被检查重置,如果是null的话,就默认是android.app.ActivityThread。
现在App的进程也创建成功了,就会进入android.app.ActivityThread的静态的main中:
public static void main(String[] args) {
    // ...
    // 初始化主线程Looper
    Looper.prepareMainLooper();
    // ...
    ActivityThread thread = new ActivityThread();
    thread.attach(false);   
    // ...
    // 启动消息循环
    Looper.loop()
    // ...
}
然后创建了一个ActivityThread,说明每当一个新的app进程被创建,都会对应一个新的ActivityThread实例,然后调用它的attach方法:
private void attach(boolean system) {
    // ...
    if (!system) {
        // ...
        final IActivityManager mgr = ActivityManagerNative.getDefault();
        // ...
        mgr.attachApplication(mAppThread);
        // ...
    }else{
        // ...
    }
    // ...
}
然后再次通过Binder IPC调用ActivityManagerProxy的attachApplication,传入的ApplicationThread(Binder)参数用于在服务端进行回调通信。最后进入ActivityManagerService::attachApplication,再调用attachApplicationLocked(thread, callingPid)。
private final boolean attachApplicationLocked(IApplicationThread thread, int pid) {
    // ...
    app = mPidsSelfLocked.get(pid);
    // ...
    app.makeActive(thread, mProcessStats);
    app.curAdj = app.setAdj= -100;
    app.curSchedGroup = app.setSchedGroup = Process.THREAD_GROUP_DEFAULT;
    app.forcingToForeground = null;
    updateProcessForegroundLocked(app, false, false);
    app.hasShownUi = false;
    app.debugging = false;
    app.cached = false;
    app.killedByAm = false;
    // ...
    thread.bindApplication(processName, appInfo, providers, app.instrumentationClass, profilerInfo, app.instrumentationArguments, app.instrumentationWatcher, app.instrumentationUiAutomationConnection, testMode, enableOpenGlTrace, enableTrackAllocation, isRestrictedBackupMode || !normalMode, app.persistent, new Configuration(mConfiguration), app.compat, getCommonServicesLocked(app.isolated), mCoreSettingsObserver.getCoreSettingsLocked());
    // ...
    if (normalMode) {
        try {
            if (mStackSupervisor.attachApplicationLocked(app)) {
                didSomething = true;
            }
        } catch (Exception e) {
            Slog.wtf(TAG, "Exception thrown launching activities in " + app, e);
            badApp = true;
        }
    }
    // ...
}
首先,通过pid获取刚刚创建的进程,然后对app进行一些初始化工作,然后调用bindApplication远程调用客户端ActivityThread::bindApplication,再通过Handler调用到ActivityThread::handleBindApplication方法:
private void handleBindApplication(AppBindData data) {
    // ...
    final ContextImpl appContext = ContextImpl.createAppContext(this, data.info);
    if (data.instrumentationName != null) {
        // ...
        mInstrumentationPackageName = ii.packageName;
        mInstrumentationAppDir = ii.sourceDir;
        mInstrumentationSplitAppDirs = ii.splitSourceDirs;
        mInstrumentationLibDir = ii.nativeLibraryDir;
        mInstrumentedAppDir = data.info.getAppDir();
        mInstrumentedSplitAppDirs = data.info.getSplitAppDirs();
        mInstrumentedLibDir = data.info.getLibDir();
        ApplicationInfo instrApp = new ApplicationInfo();
        instrApp.packageName = ii.packageName;
        instrApp.sourceDir = ii.sourceDir;
        instrApp.publicSourceDir = ii.publicSourceDir;
        instrApp.splitSourceDirs = ii.splitSourceDirs;
        instrApp.splitPublicSourceDirs = ii.splitPublicSourceDirs;
        instrApp.dataDir = ii.dataDir;
        instrApp.nativeLibraryDir = ii.nativeLibraryDir;
        LoadedApk pi = getPackageInfo(instrApp, data.compatInfo,
                appContext.getClassLoader(), false, true, false);
        ContextImpl instrContext = ContextImpl.createAppContext(this, pi);
        // ...
java.lang.ClassLoader cl = instrContext.getClassLoader();
        mInstrumentation = (Instrumentation) cl.loadClass(data.instrumentationName.getClassName()).newInstance();
        mInstrumentation.init(this, instrContext, appContext, new ComponentName(ii.packageName, ii.name), data.instrumentationWatcher, data.instrumentationUiAutomationConnection);
        // ...
    }else{
        mInstrumentation = new Instrumentation();
    }
    // ...
    Application app = data.info.makeApplication(data.restrictedBackupMode, null);
    mInitialApplication = app;
    // ...
    mInstrumentation.callApplicationOnCreate(app);
    // ...
}
首先,创建一个当前App的Context,然后如果data.instrumentationName != null,则初始化Instrumentation相关的变量,并创建Instrumentation的ApplicationInfo等对象来创建Instrumentation的Context,然后创建Instrumentation对象,并调用它的init方法进行初始化。如果data.instrumentationName == null,则new一个Instrumentation(在一个进程中只会有一个Instrumentation实例)然后创建Application对象,并调用它的onCreate方法,这样Application就会被回调了。
然后我们回到ActivityManagerService::attachApplicationLocked方法,远程执行完thread.bindApplication方法之后,接下来会调用mStackSupervisor.attachApplicationLocked(app)方法:

boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {
    // ...
    ActivityRecord hr = stack.topRunningActivityLocked(null);
    // ...
    realStartActivityLocked(hr, app, true, true)
    // ...
}
先通过topRunningActivityLocked从堆栈顶端获取要启动的Activity,然后realStartActivityLocked(hr, app, true, true):
final boolean realStartActivityLocked(ActivityRecord r,
            ProcessRecord app, boolean andResume, boolean checkConfig)
            throws RemoteException {
    // ...
    app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken, System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration), new Configuration(stack.mOverrideConfig), r.compat, r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results, newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
    // ...
}
继续通过Binder IPC远程调用scheduleLaunchActivity方法,然后进入ActivityThread的scheduleLaunchActivity方法中,然后通过Handler进入handleLaunchActivity方法:
private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent){
    // ...
    Activity a = performLaunchActivity(r, customIntent);
    
    handleResumeActivity(r.token, false, r.isForward, !r.activity.mFinished && !r.startsNotResumed);
    // ...
}
先调用performLaunchActivity方法返回一个Activity,然后调用handleResumeActivity方法让该Activity进入onResume状态。所以很显然在performLaunchActivity中肯定是生成了Activity实例,并调用了onCreate方法了,来看下代码:
private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
    // ...
    ActivityInfo aInfo = r.activityInfo;
    if (r.packageInfo == null) {
        r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo,
                Context.CONTEXT_INCLUDE_CODE);
    }
    // ...
    java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
    activity = mInstrumentation.newActivity(cl, component.getClassName(), r.intent);
    // ...
    Context appContext = createBaseContextForActivity(r, activity);
    // ...
    activity.attach(appContext, this, getInstrumentation(), r.token, r.ident, app, r.intent, r.activityInfo, title, r.parent, r.embeddedID, r.lastNonConfigurationInstances, config, r.referrer, r.voiceInteractor);
    // ...
    if (r.isPersistable()) {
     mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState);
    } else {
        mInstrumentation.callActivityOnCreate(activity, r.state);
    }
    // ...
    mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state, r.persistentState);
    // ...
    mInstrumentation.callActivityOnPostCreate(activity, r.state, r.persistentState);
    // ...
}
首先,初始化LoadedApk,然后通过Instrumentation来创建一个Activity实例,通过createBaseContextForActivity方法创建一个Activity Context,调用activity的attach方法,然后依次触发该Activity的onCreate、onRestoreInstanceState、onPostCreate等生命周期方法。
createBaseContextForActivity方法如下:
private Context createBaseContextForActivity(ActivityClientRecord r, final Activity activity) {
    // ...
    ContextImpl appContext = ContextImpl.createActivityContext(this, r.packageInfo, displayId, r.overrideConfig);
    appContext.setOuterContext(activity);
    Context baseContext = appContext;
    // ...
}
通过ContextImpl::createActivityContext创建的Context对象,可以发现,不论是System Context/App Context/Activity Context,这些Context都是通过ContextImpl生成的,具体这里再挖个坑先。
再继续进入Activity::attach方法:

final void attach(Context context, ActivityThread aThread,
            Instrumentation instr, IBinder token, int ident,
            Application application, Intent intent, ActivityInfo info,
            CharSequence title, Activity parent, String id,
            NonConfigurationInstances lastNonConfigurationInstances,
            Configuration config, String referrer, IVoiceInteractor voiceInteractor) {
    // ...
    mMainThread = aThread;
    mInstrumentation = instr;
    mToken = token;
    mIdent = ident;
    mApplication = application;
    mIntent = intent;
    mReferrer = referrer;
    mComponent = intent.getComponent();
    mActivityInfo = info;
    mTitle = title;
    mParent = parent;
    mEmbeddedID = id;
    mLastNonConfigurationInstances = lastNonConfigurationInstances;
    // ...
}
上面对Activity与ActivityThread、Instrumentation等进行了绑定,所以说每个Activity都含有一个ActivityThread引用和一个Instrumentation引用,而ActivityThread实例和Instrumentation实例在一个进程中都只有一个实例,因为ActivityThread是在进程被创建成功后,进入ActivityThread的static main()时才会被创建,而Instrumentation则是在ActivityThread被创建后进行attach的之后被创建。

总结:

启动应用流程所有方法链调用

Activity::startActivity

Activity::startActivityForResult

Instrumentation::execStartActivity:
携带参数: who:from的ContextcontextThread:from的ActivityThread的ApplicationThread,ApplicationThread中可以通过Binder IPC提供给服务端回调Activity生命周期等操作(from的主线程)。mToken:Binder类型,用来标识from的Activity,可能为null。target:from的Activity(所以是用来接收跳转结果的),如果不是从Activity跳转则为null。intent:跳转Intent。requestCode:如果是startActivity,则为-1。options:额外Bundle数据。 ActivityManagerProxy::startActivity():
携带参数: caller:上面的contextThread,from主线程。callingPackage:from的Context包名。intent:跳转Intent。resolvedType:跳转Intent的MIME类型。resultTo:上面的token,Binder类型,用来标识from的Activity。resultWho:from的Activity的mEmbeddedID(唯一标示字符串)requestCode:如果是startActivity,则为-1。startFlags:默认传入为0。profilerInfo:默认传入为null。options:额外Bundle数据。

ActivityManagerService::startActivity()(通过Binder IPC调用):
携带参数跟上面一样。

ActivityManagerService::startActivityAsUser
携带参数包括ActivityManagerService::startActivity()所有的参数,最再加一个: userId:userId,根据给当前进程分配的Linux UID(这个UID可以用来让上层系统服务进行身份识别和权限检查)得到一个userId(如果不是多用户,则直接返回0)。 ActivityStackSupervisor::startActivityMayWait()
参数: caller:上面的caller/contextThread,from主线程。callingUid:调用用户uid。callingPackage:from的Context包名。intent:跳转Intent。resolvedType:跳转Intent的MIME类型。voiceSession:传null。voiceInteractor:传null。resultTo:上面的resultTo/token,Binder类型,用来标识from的Activity。resultWho:from的Activity的mEmbeddedID(唯一标示字符串)requestCode:如果是startActivity,则为-1。startFlags:传null。profilerInfo:传null。outResult:传null。config:传null。options:额外数据。ignoreTargetSecurity:false。userId:上面的userId,根据给当前进程分配的Linux UID(这个UID可以用来让上层系统服务进行身份识别和权限检查)得到一个userId(如果不是多用户,则直接返回0)。iContainer:传null。inTask:null。 ActivityStackSupervisor::startActivityLocked():
参数: caller:上面的caller/contextThread,from主线程。intent:跳转Intent。resolvedType:跳转Intent的MIME类型。aInfo:ActivityInfo类型,解析from的Activity的Intent信息。voiceSession:传null。voiceInteractor:传null。resultTo:上面的resultTo/token,Binder类型,用来标识from的Activity。resultWho:from的Activity的mEmbeddedID(唯一标示字符串)requestCode:如果是startActivity,则为-1。callingPackage:from的Context包名。startFlags:传null。options:额外数据。ignoreTargetSecurity:false。componentSpecified:是否显示指定了component。outActivity:传null。container:上面的iContainer,转型成了ActivityContainer,还是null。inTask:null。
在这个方法中,通过ProcessRecord callerApp = mService.getRecordForAppLocked(caller);获取到之前创建的ProcessRecord,然后从Activity栈中根据resultTo/token获取到对应from Activity的ActivityRecord(sourceRecord),然后__创建将要跳转的Activity的ActivityRecord对象__(Token也是在这个时候生成的)。 ActivityStackSupervisor::startActivityUncheckedLocked():
参数: r:ActivityRecord类型,就是ActivityStackSupervisor::startActivityLocked()中创建的将要跳转的Activity的ActivityRecord对象。sourceRecord:就是ActivityStackSupervisor::startActivityLocked()方法获取的from Activity的sourceRecord。voiceSession:传null。voiceInteractor:传null。startFlags:传null。doResume:传true。options:额外数据。inTask:null。
这个方法中有大量的代码来处理task/stack等方面的逻辑,以后再仔细深入这个方法。 ActivityStrack::startActivityLocked():
方法调用者:ActivityStack类型,
targetStack:在ActivityStackSupervisor::startActivityUncheckedLocked()中确定的需要添加到的ActivityStack。
参数: r:ActivityRecord类型,就是ActivityStackSupervisor::startActivityLocked()中创建的跳转的Activity的ActivityRecord对象。newTask:是否Intent是否设置了Intent.FLAG_ACTIVITY_NEW_TASK。doResume:传true。keepCurTransition:这个具体后面再研究,跟Intent的flag有关。options:额外数据。
同样,这个方法中有大量的代码来处理task/stack等方面的逻辑,以后再仔细深入这个方法__(执行完这个方法后,ActivityRecord就会被真正加入到ActivityStack中)__。 ActivityStackSupervisor::resumeTopActivitiesLocked():
参数: targetStack:在ActivityStackSupervisor::startActivityUncheckedLocked()中确定的需要添加到的ActivityStack。target:ActivityRecord类型,就是ActivityStackSupervisor::startActivityLocked()中创建的跳转的Activity的ActivityRecord对象。targetOptions:额外数据。 ActivityStack::resumeTopActivityLocked():
参数: prev:ActivityRecord类型,就是ActivityStackSupervisor::startActivityLocked()中创建的跳转的Activity的ActivityRecord对象。options:额外数据。 ActivityStack::resumeTopActivityInnerLocked():
参数: prev:ActivityRecord类型,就是ActivityStackSupervisor::startActivityLocked()中创建的跳转的Activity的ActivityRecord对象。options:额外数据。 ActivityStackSupervisor::startSpecificActivityLocked():
参数: r:最顶部没有处于finishing的Activity,就是刚刚在startActivityLocked中加入的将要跳转的ActivityRecord,通过topRunningActivityLocked(null)查找andResume:传truecheckConfig:传true ActivityManagerService::startProcessLocked():
参数: processName:创建进程的名称,就是ActivityStack::startSpecificActivityLocked()中的r.processNameinfo:ApplicationInfo,也是r.info.applicaitonInfo,具体可以查看ActivityStackSupervisor::startActivityLocked()中创建ActivityRecord的代码。knownToBeDead:传true。intentFlags:传0。hostingType:传字符串“activity”。hostingName:ComponentName类型,intent中的ComponentnameallowWhileBooting:传false。isolated:传falsekeepIfLarge传true ActivityManagerService::startProcessLocked()(重载方法):
参数包含上面所有,多了以下几个: isolatedUid:传0abiOverride:传nullentryPoint:传nullentryPointArgs传nullcrashHandler传null
注意:在这个方法中,会创建新进程的ProcessRecord对象,并绑定ApplicationInfo等信息,这样,启动进程后进行bindApplicaiton的时候就可以根据进程PID获取到所有的ApplicationInfo信息了。 ActivityManagerService::startProcessLocked()(重载方法):
参数: app:ProcessRecord类型,创建的进程。hostingType:传字符串“activity”。hostingNameStr:通过hostingName生成的字符串(包名 + "/" + 类的简单类名)abiOverride:传nullentryPoint:传nullentryPointArgs传null Process.start()
参数(省略部分参数): processClass:上面的entryPoint,但是并不是null了,而是android.app.ActivityThread,因为在ActivityManagerService::startProcessLocked()中被设置默认值了,它表示一个类,用来作为新创建的进程的主入口,会调用这个类的静态main方法。所以启动完这个进程就会进入ActivityThread的static main()方法。zygoteArgs:fork zygote进程时的参数。

ActivityThread::main()

ActivityThread::attach()

ActivityManagerProxy::attachApplication():
参数: mAppThread:ApplicationThread()类型,Binder,用来提供给AMS调用。 ActivityManagerService::attachApplication():
参数: mAppThread:ApplicationThread()类型,Binder,用来提供给AMS调用。 ActivityManagerService::attachApplicationLocked():
参数: thread:ApplicationThread()类型,Binder,用来提供给AMS调用。上面的mAppThread。pid:当前调用的进程PID。 IApplicationThread::bindApplication():
方法调用调用者:上面的thread/mAppThread,Binder,用来提供给AMS调用。
参数(省略部分参数): packageName:用的进程名字processNameinfo:ApplicationInfo类型,从ProcessRecord中的instrumentationInfo或者info,这个ApplicationInfo是在建立ProcessRecord时就保存了。

ActivityThread::bindApplication():
参数:同上

ActivityThread::handleBindApplication():
通过Handler调用。
参数: data:AppBindData类型,里面包含的类型processName,providers,instrumentationName,instrumentationArgs,instrumentationWatcher,instrumentationUiAutomationConnection,config等数据。


ActivityManagerService中的ActivityThread::bindApplication()执行完毕之后

ActivityStackSupervisor::attachApplicationLocked():
参数: app:新建的进程绑定的ProcessRecord。 ActivityStackSupervisor::realStartActivityLocked():
参数: r:ActivityRecord类型,topRunningActivityLocked从堆栈顶端获取要启动的Activity。app:新建的进程绑定的ProcessRecord。andResume:传入truecheckConfig:传入true IApplicationThread::scheduleLaunchActivity():
参数(部分): intent:将要启动的ActivityRecord中的intent。token:将要启动的ActivityRecord中的token。info:将要启动的ActivityRecord中的ApplicationInfo。

ActivityThread::scheduleLaunchActivity():
Binder IPC调用,参数与IApplicationThread::scheduleLaunchActivity()相同。

ActivityThread::handleLaunchActivity():
该方法通过Handler调用,参数同上。 r:ActivityClientRecord类型,在ActivityThread::scheduleLaunchActivity()中封装,包括的数据有token, ident, intent, activityInfo等等,但是LoadedApk是这时根据包名从ActivityThread中弱引用缓存中获取的的。customIntent:null

ActivityThread::performLaunchActivity():
参数与ActivityThread::handleLaunchActivity()相同。

Activity::attach():
参数(部分): context:ActivityThread::performLaunchActivity()中创建的Activity Context。aThread:ActivityThread类型,主线程,一个进程都共用一个。token:构建ActivityRecord时生成的token。application:Application第一次的时候创建一遍。intent:将要启动的ActivityRecord中的intent。info:ActivityInfo类型,将要启动的ActivityRecord中的ActivityInfo。 Instrumentation::callActivityOnCreate():
参数(部分): activity:ActivityThread::performLaunchActivity()中创建的Activity。

Activity::performCreate()

Activity::onCreate()



最新发布

CentOS专题

关于本站

5ibc.net旗下博客站精品博文小部分原创、大部分从互联网收集整理。尊重作者版权、传播精品博文,让更多编程爱好者知晓!

小提示

按 Ctrl+D 键,
把本文加入收藏夹