博客
关于我
慢~再听我讲一遍Activity的启动流程
阅读量:96 次
发布时间:2019-02-26

本文共 7109 字,大约阅读时间需要 23 分钟。

Android Activity??????

??

???????????????????????????????????????????????????????????Activity??????????????????????????????????Android??????????????

??????

?????Launcher Activity????????????????????Launcher?????startActivitySafely??????????Activity?????????????????AMS?ActivityManagerService??Zygote?????????????????????????????

  • Launcher?AMS??????
  • AMS????Activity???Launcher??Paused??
  • ?????????ActivityThread
  • ActivityThread??????Application
  • ???????Activity
  • ?????????????????

    1. Launcher?AMS??????

    Launcher???????????????????????????????????????????????Launcher???startActivitySafely????????startActivity(Intent intent)?????????

    public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {  
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent, optsBundle);
    return true;
    }
    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, @Nullable Bundle options) {
    Instrumentation.ActivityResult ar = mInstrumentation.execStartActivity(
    this, mMainThread.getApplicationThread(), mToken,
    intent, requestCode, options);
    // ????
    }

    ???????????startActivity??????Instrumentation?execStartActivity??????????AMS??????????????????Activity?????Instrumentation????Instrumentation??Binder?AMS???

    2. AMS??Activity???Launcher??Paused??

    AMS????????????Binder?Zygote??????????????????????????

    public int startActivity(IApplicationThread caller, String callingPackage, String callingFeatureId,  
    Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode,
    int startFlags, ProfilerInfo profilerInfo, Bundle bOptions, int userId) {
    return startActivityAsUser(caller, callingPackage, callingFeatureId, intent, resolvedType,
    resultTo, resultWho, requestCode, startFlags, profilerInfo, bOptions, userId);
    }
    public int startActivityAsUser(IApplicationThread caller, String callingPackage, String callingFeatureId,
    Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode,
    int startFlags, ProfilerInfo profilerInfo, Bundle bOptions, int userId, boolean validateIncomingUser) {
    // ????
    userId = getActivityStartController().checkTargetUser(userId, validateIncomingUser,
    Binder.getCallingPid(), Binder.getCallingUid(), "startActivityAsUser");
    return getActivityStartController().obtainStarter(intent, "startActivityAsUser")
    .setCaller(caller)
    .setCallingPackage(callingPackage)
    .setCallingFeatureId(callingFeatureId)
    .setResolvedType(resolvedType)
    .setResultTo(resultTo)
    .setResultWho(resultWho)
    .setRequestCode(requestCode)
    .setStartFlags(startFlags)
    .setProfilerInfo(profilerInfo)
    .setActivityOptions(bOptions)
    .setUserId(userId)
    .execute();
    }
    ActivityStarter.java
    int execute() {
    // ??ActivityStack.java??startActivity??
    }
    ActivityStack.java
    private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options) {
    // ??Launcher??Paused??
    }
    ActivityStackSupervisor.java
    void startSpecificActivity(ActivityRecord r, boolean andResume, boolean checkConfig) {
    // ???????
    final WindowProcessController wpc = mService.getProcessController(r.processName, r.info.applicationInfo.uid);
    if (wpc != null && wpc.hasThread()) {
    try {
    realStartActivityLocked(r, wpc, andResume, checkConfig);
    } catch (RemoteException e) {
    // ????
    }
    }
    // ??????
    r.notifyUnknownVisibilityLaunchedForKeyguardTransition();
    final boolean isTop = andResume && r.isTopRunningActivity();
    mService.startProcessAsync(r, knownToBeDead, isTop, isTop ? "top-activity" : "activity");
    }

    ???????????AMS??ActivityTaskManagerService?Zygote?????????Zygote???????????????????ActivityThread?main???

    3. ???????ActivityThread?main????

    Zygote???????????????????????ActivityThread?main???ActivityThread?main???????Looper?ActivityThread????????????AMS??

    public static void main(String[] args) {  
    // ???Looper
    Looper.prepareMainLooper();
    // ??ActivityThread?attach
    ActivityThread thread = new ActivityThread();
    thread.attach(false, startSeq);
    // ????
    Looper.loop();
    }
    ActivityThread.java
    private void attach(boolean system, long startSeq) {
    final IActivityManager mgr = ActivityManager.getService();
    try {
    mgr.attachApplication(mAppThread, startSeq);
    } catch (RemoteException ex) {
    ex.rethrowFromSystemServer();
    }
    // ??Application
    bindApplication();
    }
    ActivityManagerService.java
    private boolean attachApplicationLocked(IApplicationThread thread, int pid, int callingUid, long startSeq) {
    // ??Application
    thread.bindApplication(processName, appInfo, providerList, instr2.mClass, profilerInfo, instr2.mArguments, instr2.mWatcher, instr2.mUiAutomationConnection, testMode, enableTrackAllocation, isRestrictedBackupMode, app.isPersistent(), config, compatInfo, services, coreSettings, buildSerial, autofillOptions, contentCaptureOptions, disabledCompatChanges);
    // ??onCreate
    didSomething = mAtmInternal.attachApplication(app.getWindowProcessController());
    }

    ???????????ActivityThread?main??????????????????????AMS???????????????

    4. ??Activity

    ??????????????????????????Activity?ActivityStackSupervisor?????????????????Activity???

    public final void bindApplication(String processName, ApplicationInfo appInfo, ProviderInfoList providerList, ComponentName instrumentationName, ProfilerInfo profilerInfo, Bundle instrumentationArgs, IInstrumentationWatcher instrumentationWatcher, IUiAutomationConnection instrumentationUiConnection, int debugMode, boolean enableBinderTracking, boolean trackAllocation, boolean isRestrictedBackupMode, boolean persistent, Configuration config, CompatibilityInfo compatInfo, Map services, Bundle coreSettings, String buildSerial, AutofillOptions autofillOptions, ContentCaptureOptions contentCaptureOptions, long[] disabledCompatChanges) {  
    // ??BIND_APPLICATION??
    sendMessage(H.BIND_APPLICATION, data);
    }
    public void handleMessage(Message msg) {
    switch (msg.what) {
    case BIND_APPLICATION:
    AppBindData data = (AppBindData) msg.obj;
    handleBindApplication(data);
    Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
    break;
    // ??case
    }
    }
    private void handleBindApplication(AppBindData data) {
    // ??Application?onCreate??
    mInstrumentation.callApplicationOnCreate(app);
    }

    ???????????ActivityStackSupervisor??????????????????Activity??????????

    ??

    ????????????????Android????????????????????????????Activity????????????????????Launcher?AMS?Zygote??ActivityThread??????????????Android????????????

    ???????????????????Android??????????????????Activity??????????????Android??????????

    转载地址:http://lkyz.baihongyu.com/

    你可能感兴趣的文章
    Netty工作笔记0060---Tcp长连接和短连接_Http长连接和短连接_UDP长连接和短连接
    查看>>
    Netty工作笔记0077---handler链调用机制实例4
    查看>>
    Netty工作笔记0084---通过自定义协议解决粘包拆包问题2
    查看>>
    Netty常见组件二
    查看>>
    netty底层源码探究:启动流程;EventLoop中的selector、线程、任务队列;监听处理accept、read事件流程;
    查看>>
    Netty核心模块组件
    查看>>
    Netty框架的服务端开发中创建EventLoopGroup对象时线程数量源码解析
    查看>>
    Netty源码—2.Reactor线程模型一
    查看>>
    Netty源码—4.客户端接入流程一
    查看>>
    Netty源码—4.客户端接入流程二
    查看>>
    Netty源码—5.Pipeline和Handler一
    查看>>
    Netty源码—6.ByteBuf原理二
    查看>>
    Netty源码—7.ByteBuf原理三
    查看>>
    Netty源码—7.ByteBuf原理四
    查看>>
    Netty源码—8.编解码原理二
    查看>>
    Netty源码解读
    查看>>
    Netty的Socket编程详解-搭建服务端与客户端并进行数据传输
    查看>>
    Netty相关
    查看>>
    Network Dissection:Quantifying Interpretability of Deep Visual Representations(深层视觉表征的量化解释)
    查看>>
    Network Sniffer and Connection Analyzer
    查看>>