android (一)、SystemServer与系统服务启动

   android的开机过程中会先启动linux内核,内核加载完之后,会启动第一个用户进程Zygote进程,Zygote主要负责孵化新的进程。Zygote启动SystemServer。
   SystemServer是android中很重要的一部分,主要负责启动android framework中的服务。SystemServer里面有几个比较重要的函数,分别是main(),init1()和init2()。
  main函数中为了保证服务的运行性能和稳定性,尽量給分配足够的内存,并且装载了c++库(android_server),且调用了init1函数。
  init1函数是本地服务,主要是从c++层面启动SurfaceFlinger本地服务。
  init2主要是启动了一个生产线程,也就是ServerThread。
SystemServer的工作分为两部分:
   一、第一部分就是加载系统中的java服务(AMS、WMS等服务),服务的加载由一个线程去处理,该线程就是ServerThread。ServerThread启动android 的java服务,并且启动了SystemUiService例如:系统栏,状态栏;
   二、SystemServer的第二部分就是启动本地系统服务(多是c++编写,如SurfaceFlinger)。

ServerThread的主体如下,大致就是创建了系统中的java服务,电量服务,窗口服务和包管理服务等等。

217             Slog.i(TAG, "Activity Manager");
218             context = ActivityManagerService.main(factoryTest);
259             ActivityManagerService.setSystemProcess();
260             
281             Slog.i(TAG, "System Content Providers");
282             ActivityManagerService.installSystemProviders();
283 
                省略若干服务
311             Slog.i(TAG, "Window Manager");
312             wm = WindowManagerService.main(context, power, display, inputManager,
313                     uiHandler, wmHandler,
314                     factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL,
315                     !firstBoot, onlyCore);
316             ServiceManager.addService(Context.WINDOW_SERVICE, wm);
317             ServiceManager.addService(Context.INPUT_SERVICE, inputManager);
318 


此方法体重比较重要的操作
   一、这段代码中相对重要的是AMS创建了系统级别的Context,并且创建了ActivityManagerService(AMS)对象(AMS的main方法调用时创建)。
   二、AMS的main方法中还干了一件比较重要的事就是创建了system-process线程(ActivityThread.systemMain()来实现)。
   三、创建了Instrumentation工具类和Application对象,这个Application映射了system-process。
   四、 ActivityManagerService.setSystemProcess()中执行了ActivityThread的俩个装载framework-android.apk文件的方法,这样一来就可以访问系 统的资源文件了。
   五、此过程中创建了ActivityMangerService和WindowManagerService(WMS)实例,WMS的实例作为了AMS的成员变量,这样AMS就可以使用WMS的成员函 数了,可以进行窗口的相关操作。

ActivityThread中装载framework-android.apk文件的两个重要方法如下:

   每个应用程序都有一个ContextImpl实例,ContextImpl是访问资源文件的和服务调用的重要入口。

1901    public ContextImpl getSystemContext() {
1902        synchronized (this) {
1903            if (mSystemContext == null) {
1904                ContextImpl context =
1905                    ContextImpl.createSystemContext(this);
1906                LoadedApk info = new LoadedApk(this, "android", context, null,
1907                        CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO);
1908                context.init(info, null, this);
1909                context.getResources().updateConfiguration(
1910                        getConfiguration(), getDisplayMetricsLocked(
1911                                Display.DEFAULT_DISPLAY,
1912                                CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO));
1913                mSystemContext = context;
1914                //Slog.i(TAG, "Created system resources " + context.getResources()
1915                //        + ": " + context.getResources().getConfiguration());
1916            }
1917        }
1918        return mSystemContext;
1919    }
1920
1921    public void installSystemApplicationInfo(ApplicationInfo info) {
1922        synchronized (this) {
1923            ContextImpl context = getSystemContext();
1924            context.init(new LoadedApk(this, "android", context, info,
1925                    CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO), null, this);
1926
1927            // give ourselves a default profiler
1928            mProfiler = new Profiler();
1929        }
1930    }
1931


猜你喜欢

转载自blog.csdn.net/jiabailong/article/details/42557373
今日推荐