anroid SystemServer启动中调用ActivityManagerService.main(int factoryTest)流程

[1]android系统启动第一次调用ActivityManagerServicemain方法设置系统上下环境,

并保存在 mContext成员变量中

SystemServer启动后会调用ActivityManagerService中的main函数:

frameworks/base/services/java/com/android/server/am/ActivityManagerService.java

    publicstatic final Context main(int factoryTest){

        AThread thr =new AThread();//new了一个ActivityManagerService类保存在成员变量mService

        thr.start();

 

        synchronized (thr){

            while(thr.mService == null){

                try{

                    thr.wait();

                }catch(InterruptedException e){

                }

            }

        }

 

        ActivityManagerService m = thr.mService;//AThread中的成员变量mServiceActivityManagerServer成员变量m

        mSelf = m;  //ActivityManagerServer对象m 附值给成员变量mSelf

        ActivityThread at = ActivityThread.systemMain();//启动一个systemMain ActivityThread

        mSystemThread = at;

        Context context = at.getSystemContext();  //systemMain的上下环境给context变量,之后再传给ActivityManagerServer成员变量mContext

        m.mContext = context;  //附值给mContext

        m.mFactoryTest = factoryTest;

        m.mMainStack =new ActivityStack(m, context,true);

 

        m.mBatteryStatsService.publish(context);

        m.mUsageStatsService.publish(context);

 

        synchronized (thr){

            thr.mReady =true;

            thr.notifyAll();

        }

 

        m.startRunning(null, null, null, null);

 

        return context;

    }

 

[2] ActivityThread.systemMain:

framework/base/core/java/android/app/ActivityThread.java

publicstatic final ActivityThread systemMain(){

    ActivityThread thread = new ActivityThread();//调用一个空的构造函数,生成对象,系统线程,此线程在Home Activity创建之前生成.

    thread.attach(true);//

    return thread;

}

[3]执行attach初始化context

 

    private final void attach(boolean system){

        sThreadLocal.set(this);

        mSystemThread = system;

        if(!system){

            ViewRoot.addFirstDrawHandler(new Runnable(){

                publicvoid run(){

                    ensureJitEnabled();

                }

            });

            android.ddm.DdmHandleAppName.setAppName("<pre-initialized>");

            RuntimeInit.setApplicationObject(mAppThread.asBinder());

            IActivityManager mgr = ActivityManagerNative.getDefault();

            try{

                mgr.attachApplication(mAppThread);

            }catch(RemoteException ex){

            }

        }else{

            // Don't set application object here -- if the system crashes,

            // we can't display an alert, we just want to die die die.

            android.ddm.DdmHandleAppName.setAppName("system_process");

            try{

                mInstrumentation =new Instrumentation();

                ContextImpl context =new ContextImpl();

                context.init(getSystemContext().mPackageInfo, null, this);

                Application app = Instrumentation.newApplication(Application.class, context);

                mAllApplications.add(app);

                mInitialApplication = app;

                app.onCreate(); //启动app

            }catch(Exception e){

                thrownew RuntimeException(

                        "Unable to instantiate Application():"+ e.toString(), e);

            }

        }

 

        ViewRoot.addConfigCallback(new ComponentCallbacks(){

            publicvoid onConfigurationChanged(Configuration newConfig){

                synchronized (mPackages){

                    // We need to apply this change to the resources

                    // immediately, because upon returning the view

                    // hierarchy will be informed about it.

                    if(applyConfigurationToResourcesLocked(newConfig)){

                        // This actually changed the resources!  Tell

                        // everyone about it.

                        if(mPendingConfiguration == null ||

                                mPendingConfiguration.isOtherSeqNewer(newConfig)){

                            mPendingConfiguration = newConfig;

 

                            queueOrSendMessage(H.CONFIGURATION_CHANGED, newConfig);

                        }

                    }

                }

            }

            publicvoid onLowMemory(){

            }

        });

    }

 

[4]接着看getSystemContext:

    public ContextImpl getSystemContext(){

        synchronized (this){

            if(mSystemContext == null){

                ContextImpl context =ContextImpl.createSystemContext(this);

//调用ContextImpl静态方法,生成当前对象的上下环境对象

                LoadedApk info = new LoadedApk(this, "android", context, null);

                context.init(info, null,this);

                context.getResources().updateConfiguration(

                        getConfiguration(), getDisplayMetricsLocked(false));

                mSystemContext = context;

                //Slog.i(TAG, "Created system resources " + context.getResources()

                //        + ": " + context.getResources().getConfiguration());

            }

        }

        return mSystemContext;

    }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值