美文网首页
APK 启动流程

APK 启动流程

作者: 小虫虫奇遇记 | 来源:发表于2020-08-16 16:32 被阅读0次

    冷启动

    1. Launcher 点击App图标:
      Launcher.java
    onclick():
    startActivitySafely()
    super.startActivity
    

    2.Activity.java

    Instrumentation.ActivityResult ar =
                    mInstrumentation.execStartActivity(
                        this, mMainThread.getApplicationThread(), mToken, this,
                        intent, requestCode, options);
    
    1. Instrumentation.java
      public ActivityResult execStartActivity(
                Context who, IBinder contextThread, IBinder token, Activity target,
                Intent intent, int requestCode, Bundle options) {
            IApplicationThread whoThread = (IApplicationThread) contextThread;  
            try {
                intent.migrateExtraStreamToClipData();
                intent.prepareToLeaveProcess(who);
                //ActivityManagerService
                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;
        }
    
    1. ActivityManagerService.java
      mActivityStarter = new ActivityStarter(this, mStackSupervisor);
        protected ActivityStackSupervisor createStackSupervisor() {
            return new ActivityStackSupervisor(this, mHandler.getLooper());
        }
    
        final int startActivity(Intent intent, ActivityStackSupervisor.ActivityContainer container) {
             ....
            intent.addFlags(FORCE_NEW_TASK_FLAGS);
            return mActivityStarter.startActivityMayWait(null, -1, null, intent, mimeType, null, null,
                    null, null, 0, 0, null, null, null, null, false, userId, container, null,
                    "startActivity");
        }
    

    5.ActivityStarter.java
    ActivityStackSupervisor.java
    Zygote进程孵化出新的应用进程后,通过反射执行ActivityThread类的main方法。在该方法里会先准备好Looper和消息队列,然后调用attach方法将应用进程绑定到AMS,然后进入loop循环,不断地读取消息队列里的消息,并分发消息。
    //ActivityThread

    public static void main(String[] args) {
    ``````
    //准备主线程的Looper,分析Handler,Looper
    Looper.prepareMainLooper();

    //创建当前进程的ActivityThread
    ActivityThread thread = new ActivityThread();
    
    //将该进程绑定到AMS
    thread.attach(false);
    
    if (sMainThreadHandler == null) {
    //保存进程对应的主线程Handler
        sMainThreadHandler = thread.getHandler();
    }
    
    ``````
    //进入主线程的消息循环
    Looper.loop();
    
    ``````
    

    }

    //上面说过,ApplicationThread是ActivityThread用来与AMS通讯的中介
    final ApplicationThread mAppThread = new ApplicationThread();

    private void attach(boolean system) {
    if (!system) {
    final IActivityManager mgr = ActivityManagerNative.getDefault();

        //调用AMS的attachApplication方法,将ApplicationThread对象绑定至ActivityManagerService
        //这样AMS就可以通过ApplicationThread代理对象控制应用进程
    
            mgr.attachApplication(mAppThread);
    } else {
        ``````
    }
    

    }

    https://blog.csdn.net/qian520ao/article/details/78156214

    ActivityThread.java

    handleLaunchActivity:
        performLaunchActivity:onCreate,onStart,onRestoreInstance
        handleResumeActivity: performResumeActivity   r.activity.performResume();
    
    

    相关文章

      网友评论

          本文标题:APK 启动流程

          本文链接:https://www.haomeiwen.com/subject/zacwdktx.html