The Systemserver process is the first process hatched by zygote, which starts with the invocation of Startsystemserver () from the main function of Zygoteinit.java. The difference from starting a normal process is that the class zygote provides a specialized function startsystemserver () for the startup Systemserver, rather than the standard forandspecilize function. At the same time, the first thing to do when the systemserver process starts is different from the normal process.
the key functions of the function startsystemserver () are as follows:
(1) defines a string[] array that contains information about the process to start , with the last item specifying the first Java class to be loaded after the new process is started, where the class Com.android.SystemServer
(2) call Forksystemserver () to hatch a new process from the current zygote process . The function is a native function, and its function is similar to that of Folkandspecilize ()
(3) after starting a new process , the following two events are mainly completed in function handlesystemserverprocess ().
1. close the socket server
2. execute the Com.android.server.SystemServer class in the function main ().
In addition to these two main things, there are some additional configuration of the runtime environment, which are mainly done in function commoninit () and function zygoteinitnative (). Once you have configured the Systemserver process environment, run from the main () function in class Systemserver.
1. Start various system service threads
The systemserver process plays a central role in the Android environment , and most of the system services that can interact directly in the APK app run in this process, such as Windowmanagerserver (WMS), Common applications such as activitymanagersystemservive (AMS), Packagemanagerserver (PMS), which are present in a single thread and systemserver processes. The following is a description of what service processes are available and the order in which they are started.
The main () function in systemserver first calls the function init1 (), which is a native function that internally carries out some initialization work related to Dalvik virtual machines. When the function is finished, it calls the Java-side init2 () function inside, which is why there is no reference to Init2 () in the Java source, and the primary system service is done in the Init2 () function.
The function first creates a Serverthread object, which is a thread, and then runs the thread directly, starting from within the Serverthread run () method to actually start the various service threads. Basically each service corresponds to the Java class, from the point of view of coding specifications, the start of these service patterns can be categorized as the following three kinds:
(1) Mode 1: A service is constructed directly using a constructor, and since most services correspond to one thread, a thread is created inside the constructor and runs automatically.
(2) Mode 2: Refers to the service class will provide a getinstance () method to obtain the service object through this method, the benefit is to ensure that the system contains only one of the service object.
(3) Mode 3: Refers to the service class that starts execution in the main () function.
Start the list of services in Systemserver:
Service class name |
Function description |
Startup mode |
Entropyservice |
Provide pseudo-random number |
1.0 |
Powermanagerservice |
Power Management Services |
1.2/3 |
Activitymanagerservice |
One of the most core services to manage Activity |
Custom |
Telephonyregistry |
Event response of the phone module registered through the service, such as restart, shutdown, boot, etc. |
1.0 |
Packagemanagerservice |
Package Management Services |
3.3 |
Accountmanagerservice |
Account Management Service refers to the contact account, not the Linux system account |
1.0 |
Contentservice |
ContentProvider services, providing cross-process data exchange |
3.0 |
Batteryservice |
Battery Management Services |
1.0 |
Lightsservice |
Natural light intensity Sensing sensor service |
1.0 |
Vibratorservice |
Vibrator Service |
1.0 |
Alarmmanagerservice |
Timer Management Service, provide timing reminder service |
1.0 |
Windowmanagerservice |
One of the core services of the Framework, responsible for window management |
3.3 |
Bluetoothservice |
Bluetooth service |
1.0 + |
Devicepolicymanagerservice |
Provides some system-level settings and properties |
1.3 |
Statusbarmanagerservice |
Status bar Management Service |
1.3 |
Clipboardservice |
system Clipboard Service |
1.0 |
Inputmethodmanagerservice |
Input Method Management Service |
1.0 |
Netstatservice |
Network Status Service |
1.0 |
Networkmanagementservice |
Network Management Services |
Nms.create () |
Connectivityservice |
Network Connection Management Services |
2.3 |
Throttleservice |
It's not clear how it works. |
1.3 |
(Continuation of table)
Service class name |
Function description |
Startup mode |
Accessibilitymanagerservice |
The secondary management program intercepts all user input and, based on the Some input gives the user some extra feedback, which plays an auxiliary effect |
1.0 |
Mountservice |
Mount service, which calls the Linux-level Mount program through the service |
1.0 |
Notificationmanagerservice |
Notification Bar Management Service, notification bar and form in Android On the left side of the interface, which is on the right. |
1.3 |
Devicestoragemonitorservice |
Disk Space Status Detection service |
1.0 |
Locationmanagerservice |
Location Services |
1.3 |
Searchmanagerservice |
Search administration Services |
1.0 |
Dropboxmanagerservice |
Access the Linux-level Dropbox program through this service |
1.0 |
Wallpapermanagerservice |
Wallpaper Management Service, wallpaper is not equivalent to the desktop background, Inside the View system, the wallpaper can be used as a background for any window |
1.3 |
Audioservice |
Audio Management Services |
1.0 |
Backupmanagerservice |
System Backup Service |
1.0 |
Appwidgetservice |
Widget Services |
1.3 |
Recognitionmanagerservice |
Identity recognition Service |
1.3 |
Diskstatsservice |
Disk Statistics Service |
1.0 |
The AMS startup mode is as follows:
Call the main () function to return a context object instead of the AMS service itself.
Call Ams.setsystemprocess ().
Call Ams.installproviders ().
Call Systemready () and when AMs finishes executing Systemready (), the Systemready () function of the associated service is started successively, completing the overall initialization.
Note:
Ystemserver is a core process of the Android system that was created by the zygote process, so it is behind Zygote during Android startup. All of Android's service loops are built on top of systemserver. In Systemserver, it will be possible to see that it builds up most of the services in Android and adds these services to ServiceManager svclist through the Servermanager Add_service method. To complete the Servciemanager service management.
First look at the main function of Systemserver:
[Java] View plaincopy
- Native public static void Init1 (String[]args);
- Public static void Main (string[] args) {
- if (samplingprofilerintegration.isenabled ()) {
- Samplingprofilerintegration.start ();
- Timer = new timer ();
- Timer.schedule (new TimerTask () {
- @Override
- public Void Run () {
- Samplingprofilerintegration.writesnapshot ("System_server");
- }
- }, Snapshot_interval,snapshot_interval);
- }
- //The system server has a to run all ofthe time, so it needs to be
- //As efficient as possible with itsmemory usage.
- Vmruntime.getruntime (). Settargetheaputilization (0.8f);
- System.loadlibrary ("android_servers"); //Load local library android_servers
- Init1 (args);
- }
In the main function is called the Local method init1 (args), his implementation is located in the. In/base/services/jni/com_android_server_systemservice.cpp
[Java] View plaincopy
- Static voidandroid_server_systemserver_init1 (jnienv* env, Jobject clazz)
- {
- System_init ();
- }
Take a closer look at System_init, where we see the closed Loop management framework:
[Java] View plaincopy
- Runtime->callstatic ("Com/android/server/systemserver","Init2"); Callback the Init2 method in the Systemserver.java
- if (proc->supportsprocesses ()) {
- Logi ("System server:enteringthread pool.\n");
- Processstate::self ()->startthreadpool ();
- Ipcthreadstate::self ()->jointhreadpool ();
- Logi ("System server:exitingthread pool.\n");
- }
The service registration is completed by calling the Init2 method in Com/android/server/systemserver.java. In the Init2 method, the main establishment is to serverthread the thread, and then start the thread to complete the service registration.
[Java] View plaincopy
- Public static final void Init2 () {
- SLOG.I (TAG, "entered the Androidsystem server!");
- Thread thr = new Serverthread ();
- Thr.setname ("Android.server.ServerThread");
- Thr.start ();
- }
To implement the service registration in the Serverthread Run method:
[Java] View plaincopy
- try {
- SLOG.I (TAG, "Entropyservice");
- Servicemanager.addservice ("Entropy", new Entropyservice ());
- SLOG.I (TAG, "PowerManager");
- Power = new Powermanagerservice ();
- Servicemanager.addservice (Context.power_service, POWER);
- SLOG.I (TAG, "Activitymanager");
- Context =activitymanagerservice.main (factorytest);
- SLOG.I (TAG, "Telephonyregistry");
- Servicemanager.addservice ("Telephony.registry", Newtelephonyregistry (context));
- }
2. Start the first activity:
When the above service thread is started, the Activitymanagerservice (AMS) service is completed with the Systemready () call to finish the last boot, The last piece of code inside the AMS function Systemready () issues the topmost activity message in the start task queue. Because there are no activity objects in the Mmainstack queue when the system is just started, starthomeactivitylocked () is called in class Acitivitystack.
When powered on, the activity that the system starts to perform is entirely dependent on the first activity object in the Mmainstack queue. If you can construct an activity object at activitymanagerserver startup (not to construct an activity class object) and place it in the Mmainstack queue, So the first activity object to run is the activity, unlike other operating systems that set up a fixed program as the first startup program.
In AMS's starthomeacitivitylocked (), the system issues a catagory field that contains the intent of category_home.
No matter which application, just declare that you can intent, then you can be considered the home program, which is why there are various "home programs" in the Android field. The system does not have any program to give "Home" privileges, but to give the right to the user. When there are multiple programs in the system that should intent, the system pops up a dialog box that asks the user to choose which program to start, and allows the user to remember the selection, so that the same activity will be started each time they press home, which is the first acitivity start-up process.
Android (Java) Learning Note 161:framework The startup Systemserver process of the running environment