android (1), SystemServer and system service start

   During the boot process of Android, the Linux kernel will be started first. After the kernel is loaded, the first user process Zygote will be started. Zygote is mainly responsible for incubating new processes. Zygote starts SystemServer.
   SystemServer is a very important part of android and is mainly responsible for starting services in the android framework. There are several important functions in SystemServer, namely main(), init1() and init2().
  In the main function, in order to ensure the running performance and stability of the service, try to allocate enough memory, load the c++ library (android_server), and call the init1 function.
  The init1 function is a local service, mainly to start the SurfaceFlinger local service from the C++ level.
  init2 mainly starts a production thread, which is ServerThread.
The work of SystemServer is divided into two parts:
   1. The first part is to load java services (AMS, WMS and other services) in the system. The loading of services is handled by a thread, which is ServerThread. ServerThread starts android's java service, and starts SystemUiService such as: system bar, status bar;
   2. The second part of SystemServer is to start the local system service (mostly written in C++, such as SurfaceFlinger).

The main body of ServerThread is as follows, roughly creating the java service, power service, window service and package management service in the system.

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 


The weight of this method is more important operation
   1. The relatively important thing in this code is that AMS creates a system-level Context and creates an ActivityManagerService (AMS) object (created when the main method of AMS is called).
   Two, the main method of AMS also did a more important thing is to create a system-process thread (ActivityThread.systemMain () to achieve).
   3. The Instrumentation tool class and Application object are created, and this Application maps the system-process.
   Fourth, in ActivityManagerService.setSystemProcess(), the two methods of loading the framework-android.apk file of ActivityThread are executed, so that the resource files of the system can be accessed.
   5. In this process, instances of ActivityMangerService and WindowManagerService (WMS) are created, and the instances of WMS are used as member variables of AMS, so that AMS can use the member functions of WMS and perform window-related operations.

Two important methods for loading the framework-android.apk file in ActivityThread are as follows:

   Every application has an instance of ContextImpl, which is an important entry point for accessing resource files and calling services.

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


Guess you like

Origin blog.csdn.net/jiabailong/article/details/42557373