• SystemServer是如何启动AMS的


    启动Binder线程池和SystemServiceManager,并且启动各种系统服务


    SystemServer.main()

    初始化SystemServer对象,然后调用run()

    new SystemServer().run()
    
    • 1

    SystemServer.run()

    //其他代码省略
     createSystemContext();//加载系统资源
     startBootstrapServices(t);//启动引导服务
     startCoreServices(t);//启动核心服务
     startOtherServices(t);//启动其他服务
    
    • 1
    • 2
    • 3
    • 4
    • 5

    SystemServer.createSystemContext()

    
    //系统资源加载 
    ActivityThread activityThread = ActivityThread.systemMain();
    mSystemContext = activityThread.getSystemContext();//ContextImpl
    mSystemContext.setTheme(DEFAULT_SYSTEM_THEME);
    
    final Context systemUiContext = activityThread.getSystemUiContext();
    systemUiContext.setTheme(DEFAULT_SYSTEM_THEME);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    ActivityThread.systemMain()

    //ResourcesManager.getInstance()获取资源管理实例
    ActivityThread thread = new ActivityThread();
    
    thread.attach(true, 0);
    return thread;
    
    • 1
    • 2
    • 3
    • 4
    • 5

    thread.attach(true, 0);

    
    mInstrumentation = new Instrumentation();
    mInstrumentation.basicInit(this);
    /**
    getSystemContext()单例模式创建ContextImpl对象mSystemContext-->createSystemContext-->创建LoadedApk对象(创建ApplicationInfo(),创建ClassLoader)
    createAppContext()利用刚创建的LoadedApk对象创建新的ContextImpl对象
    **/
    ContextImpl context = ContextImpl.createAppContext(this,getSystemContext().mPackageInfo);
    /**
    initializeJavaContextClassLoader() 设置当前的线程ContextClassLoader
    newApplication()
    public Application newApplication(ClassLoader cl, String className, Context context)throws InstantiationException, IllegalAccessException, ClassNotFoundException {
        //创建Application对象
        Application app = getFactory(context.getPackageName())
            .instantiateApplication(cl, className);
        //将新创建的ContextImpl对象保存到Application父类成员变量mBase
        //将新创建的LoadedApk对象保存到Application的成员变量mLoadedApk
        app.attach(context);
        return app;
        }
    **/
    mInitialApplication = context.mPackageInfo.makeApplication(true, null);
    mInitialApplication.onCreate();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    SystemServer.startBootstrapServices()

    
    // SystemServiceManager 专门管理各种服务启动(java层各种服务)
    
    ActivityTaskManagerService atm = mSystemServiceManager.startService(
            ActivityTaskManagerService.Lifecycle.class).getService();
            
    // 在SystemServiceManager.startService()中new Lifecycle()-->new ActivityManagerService(),且回调Lifecycle.onStart()
    mActivityManagerService = ActivityManagerService.Lifecycle.startService(
            mSystemServiceManager, atm);
    mActivityManagerService.setSystemServiceManager(mSystemServiceManager);
    //设置AMS的APP安装器
    mActivityManagerService.setInstaller(installer);
    //开启PMS服务
    mPowerManagerService = mSystemServiceManager.startService(PowerManagerService.class);
    //初始化AMS相关的PMS服务
    mActivityManagerService.initPowerManagement();
    //添加C/C++各种服务
    mActivityManagerService.setSystemProcess();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    new ActivityManagerService(context, sAtm)

    • 启动相关服务
    • 创建UI线程
    • 创建ActiveServices
    • 创建CpuTracker线程

    Lifecycle.start()

    //移除所有的进程组
    removeAllProcessGroups();
    //启动CpuTracker线程
    mProcessCpuThread.start();
    //启动电池统计服务
    mBatteryStatsService.publish();
    //启动APP操作信息服务
    mAppOpsService.publish();
    //添加到LocalServices中
    LocalServices.addService(ActivityManagerInternal.class, mInternal);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    ActivityManagerService.setSystemProcess();

    /**
    ServiceManager c/c++服务
    
    activity AMS
    procstats 进程统计
    meminfo 内存信息
    gfxinfo 图像信息
    dbinfo 数据库
    cpuinfo 
    permission
    processinfo 进程信息
    cacheinfo 缓存信息
    **/
    ServiceManager.addService(Context.ACTIVITY_SERVICE, this, /* allowIsolated= */ true,DUMP_FLAG_PRIORITY_CRITICAL | DUMP_FLAG_PRIORITY_NORMAL | DUMP_FLAG_PROTO);
    ServiceManager.addService(ProcessStats.SERVICE_NAME, mProcessStats);
    ServiceManager.addService("meminfo", new MemBinder(this), /* allowIsolated= */ false, DUMP_FLAG_PRIORITY_HIGH);
    ServiceManager.addService("gfxinfo", new GraphicsBinder(this));
    ServiceManager.addService("dbinfo", new DbBinder(this));
    if (MONITOR_CPU_USAGE) {
    ServiceManager.addService("cpuinfo", new CpuBinder(this),
        /* allowIsolated= */ false, DUMP_FLAG_PRIORITY_CRITICAL);
        }
    ServiceManager.addService("permission", new PermissionController(this));
    ServiceManager.addService("processinfo", new ProcessInfoService(this));
    ServiceManager.addService("cacheinfo", new CacheBinder(this));
    /**
    getSystemContext().installSystemApplicationInfo(info, classLoader);
    getSystemUiContext().installSystemApplicationInfo(info, classLoader);
    mProfiler = new Profiler();
    **/
    mSystemThread.installSystemApplicationInfo(info, getClass().getClassLoader());
    //创建ProcessRecord对象
    ProcessRecord app = mProcessList.newProcessRecordLocked(info, info.processName,
                            false,
                            0,
                            new HostingRecord("system"));
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    ActivityThread.installSystemApplicationInfo(info,getClass().getClassLoader());

    //最终调用LoadedApk的installSystemApplicationInfo(),加载名为android的包
    getSystemContext().installSystemApplicationInfo(info, classLoader);
    getSystemUiContext().installSystemApplicationInfo(info, classLoader);
    //创建用于性能统计Profiler对象
    mProfiler = new Profiler();
    
    • 1
    • 2
    • 3
    • 4
    • 5

    SystemServer.startOtherServices(t);

    //与AMS相关,其他代码省略
    /**
    安装系统Provider
    创建CoreSettingsObserver,用于监控Settings的改变
    **/
    mActivityManagerService.installSystemProviders();
    //
    wm = WindowManagerService.main(context, inputManager, !mFirstBoot, mOnlyCore,new PhoneWindowManager(),mActivityManagerService.mActivityTaskManager);
    //加入到底层服务中
    ServiceManager.addService(Context.WINDOW_SERVICE, wm, /* allowIsolated= */ false, DUMP_FLAG_PRIORITY_CRITICAL | DUMP_FLAG_PROTO);
    //WMS管理
    mActivityManagerService.setWindowManager(wm);
    /**
    startSystemUi()启动系统UI
    执行一系列服务的systemReady()
    **/
    mActivityManagerService.systemReady();
    
    //至此80多个服务初始化完成
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
  • 相关阅读:
    ChatGPT推出全新功能,引发人工智能合成声音担忧|百能云芯
    【实用软件】电脑wifi密码查看器
    08_SpingBoot 集成Redis
    3DMAX金属屋顶墙面铺设插件使用方法
    FullCalendarDemo5 控件的实例讲解—拖拽实现值班排班(五)
    NIO Netty(四)
    生产环境p0级故障:用户钱付了,订单还是显示未支付,用户把我们投诉了!
    leetcode hot100 每日温度
    【吴恩达L1W2作业2】有关scipy.misc.imresize
    vue 部署到nginx 刷新404
  • 原文地址:https://blog.csdn.net/Super_666/article/details/126014557