Android 应用程序的启动流程

参考: http://gityuan.com/2016/03/12/start-activity/Android 应用程序的启动流程_第1张图片

1.应用程序启动流程

  1. 点击桌面应用,startActivity()函数最终是由Instrumentation通过Android的Binder跨进程通信机制 发送消息给 system_server 进程;
  2. 在 system_server 中,启动进程的操作由ActivityManagerService 通过 socket 通信告知 Zygote
  3. Zygote进程 fork 子进程(app进程)。app 进程启动后, 执行其ActivityThread-main()函数:创建 `Looper,ActivityThread 对象,并开启主线程消息循环Looper.loop()。
  4. 调用 ActivityThread#attach(false)方法进行 Binder 通信,通知system_server进程执行ActivityManagerService#attachApplication(mAppThread)方法,用于初始化Application和Activity。
  5. system_server进程在收到请求后,进行一系列准备工作后,再通过binder IPC向App进程发送scheduleLaunchActivity请求;
  6. App进程的binder线程(ApplicationThread)在收到请求后,通过handler向主线程发送LAUNCH_ACTIVITY消息;
  7. 主线程在收到Message后,通过发射机制创建目标Activity,并回调Activity.onCreate()等方法

2.代码

ActivityThread

 public static void main(String[] args) {
       
		//创建loop
        Looper.prepareMainLooper();
// 		创建自身
        ActivityThread thread = new ActivityThread();
        thread.attach(false, startSeq);//绑定

        if (sMainThreadHandler == null) {
            sMainThreadHandler = thread.getHandler();
        }
        // End of event ActivityThreadMain.
        Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
        Looper.loop();

        throw new RuntimeException("Main thread loop unexpectedly exited");
    }
  private void attach(boolean system, long startSeq) {
	  	final IActivityManager mgr = ActivityManager.getService();
	    //创建activity对象。      
	    mgr.attachApplication(mAppThread, startSeq);
		
	    mInstrumentation = new Instrumentation();
		mInstrumentation.basicInit(this);
		//创建context
	    ContextImpl context = ContextImpl.createAppContext(this, getSystemContext().mPackageInfo);
	    mInitialApplication = context.mPackageInfo.makeApplication(true, null);//创建appliaction
		//执行onCreate()
	    mInitialApplication.onCreate();
	}

IActivityManager的实现类是ActivityManagerService AMS
makeApplication 是创建application的方法

1.创建application对象

 public Application makeApplication(boolean forceDefaultAppClass,
            Instrumentation instrumentation) {

 			java.lang.ClassLoader cl = getClassLoader();
         
            ContextImpl appContext = ContextImpl.createAppContext(mActivityThread, this);
            app = mActivityThread.mInstrumentation.newApplication(    cl, appClass, appContext);
            appContext.setOuterContext(app);

}

 public Application newApplication(ClassLoader cl, String className, Context context)
            throws InstantiationException, IllegalAccessException, 
            ClassNotFoundException {
        Application app = getFactory(context.getPackageName())
                .instantiateApplication(cl, className);
        app.attach(context);
        return app;
    }
  public @NonNull Application instantiateApplication(@NonNull ClassLoader cl,
            @NonNull String className)
            throws InstantiationException, IllegalAccessException, ClassNotFoundException {
        return (Application) cl.loadClass(className).newInstance();
    }

最终通过反射构造函数的方式创建application对象。

2.创建Activity对象

boolean attachApplicationLocked(ProcessRecord app) throws RemoteException {

   final String processName = app.processName;
        boolean didSomething = false;
        for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
            final ActivityDisplay display = mActivityDisplays.valueAt(displayNdx);
            for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
               ...
                final ActivityRecord top = stack.topRunningActivityLocked();
                final int size = mTmpActivityList.size();
                for (int i = 0; i < size; i++) {
                    final ActivityRecord activity = mTmpActivityList.get(i);
 ...
                            if (realStartActivityLocked(activity, app,
                                    top == activity /* andResume */, true /* checkConfig */)) {
                                didSomething = true;
                            }
                      
                    }
                }
            }
        }
      

} 

你可能感兴趣的:(FrameWork)