The heart of the Android system, the relationship between the SystemServer process and various system services

The heart of the Android system, the relationship between the SystemServer process and various system services

SystemServer

SystemServer is an important process in the Android system. It is the first process of zygote fork and is responsible for starting and managing various services in the system. In the Android system, the name of the SystemServer process is "system_server".

After the SystemServer process is started, the SystemServer class is loaded and its main function is executed. The main function is the entry point of SystemServer and is responsible for starting and initializing various system services. In this process, SystemServer creates a Looper and a Handler to process messages and run tasks in the main thread. Looper is part of the Android event loop and is responsible for receiving and distributing messages in the main thread, while Handler is used to send and process messages.

The system services created and initialized by SystemServer include ActivityManagerService (AMS), PackageManagerService (PMS), WindowManagerService (WMS), etc. These services play an important role in the Android system. For example, AMS is responsible for managing the life cycle and activity status of applications, PMS is responsible for managing the installation and uninstallation of application packages, and WMS is responsible for managing the creation, display, and update of windows.

In the main function of SystemServer, various other system services are also started, which are managed and controlled by SystemServiceManager. The SystemServer process will always run, processing messages from various system services to ensure the normal operation and stability of the system.

ServiceManager

ServiceManager is an important daemon process in the Android system, responsible for managing the registration, search and startup of system services.

  1. "effect":

Provide service registration and lookup: ServiceManager acts as a central registry, allowing applications and system components to register themselves as services and provide a unique service name. Other applications can find and obtain registered services through ServiceManager, thereby achieving inter-process communication.

Starting and managing system processes: ServiceManager is also responsible for starting and managing some important system processes, such as system services (such as Telephony services, Media services, etc.), and some other important system components.

Implement the Binder mechanism: The Android system uses Binder as the inter-process communication (IPC) mechanism, and ServiceManager is one of the key components of Binder communication.

  1. 「Permissions」:

ServiceManager has high access permissions. Generally, only system-level applications or applications with system permissions can use ServiceManager to register and query services.

  1. 「Relationship with Binder」:

ServiceManager is the daemon process of Binder. If ServiceManager crashes on Android, all process services that use Binder communication will be affected. ServiceManager itself is also a Binder service, and its handle is fixed to 0. If an application wants to send data to a service through Binder, it must first obtain the handle of the service through ServiceManager, and then communicate with the service through the binder driver.

  1. 「Start the process」:

During the startup of the Android system, the SystemServer process starts the ServiceManager and registers various system services in the ServiceManager.

 protected final void publishBinderService(String name, IBinder service, boolean allowIsolated) { ServiceManager.addService(name, service, allowIsolated); }

SystemServiceManager

SystemServiceManager is a class used to manage system services in the Android system. It is responsible for managing all registered system-level services and starting and stopping them when needed. For example, when the SystemServer process starts, SystemServiceManager will register services such as PackageManagerService, WindowManagerService, and ActivityManagerService, and start them when needed.

 @SuppressWarnings("unchecked") public <T extends SystemService> T startService(Class<T> serviceClass) { final String name = serviceClass.getName(); Slog.i(TAG, "Starting " + name); // Create the service. if (!SystemService.class.isAssignableFrom(serviceClass)) { throw new RuntimeException("Failed to create " + name + ": service must extend " + SystemService.class.getName()); } final T service; try { Constructor<T> constructor = serviceClass.getConstructor(Context.class); service = constructor.newInstance(mContext); } catch (InstantiationException ex) { throw new RuntimeException("Failed to create service " + name + ": service could not be instantiated", ex); } catch (IllegalAccessException ex) { throw new RuntimeException("Failed to create service " + name + ": service must have a public constructor with a Context argument", ex); } catch (NoSuchMethodException ex) { throw new RuntimeException("Failed to create service " + name + ": service must have a public constructor with a Context argument", ex); } catch (InvocationTargetException ex) { throw new RuntimeException("Failed to create service " + name + ": service constructor threw an exception", ex); } // Register it. mServices.add(service); // Start it. try { service.onStart(); } catch (RuntimeException ex) { throw new RuntimeException("Failed to start service " + name + ": onStart threw an exception", ex); } return service; }

SystemServiceManager registers system services by calling the registerService function, and can start or stop these services through the startService and stopService functions. When the system no longer needs a service, SystemServiceManager will also be responsible for stopping and uninstalling it.

SystemServiceManager is a component used internally by the Android system and usually does not require developers to interact with it directly.

SystemService

SystemService is a service for some corresponding functions of the framework, which is called by other modules and apps. These services are usually related to specific functions or modules, such as BatteryService (used to obtain battery properties, charging status, percentage, etc.), PowerManagerService (control sleep, wake-up, etc.) and TvInputManagerService (used to create and release sessions, etc.).

The use of SystemService is relatively simple. Get a manager object through context.getSystemService(@NonNull Class<T> serviceClass) or Object getSystemService(@ServiceName @NonNull String name) method, and then call the method in SystemService.

SystemService is a component used within the Android system. When developers develop Android applications, they usually interact with system services through the API provided by the system rather than directly operating SystemService.

 val powerManager = getSystemService(Context.POWER_SERVICE) as PowerManager

<<:  iOS 17.5 official version released, do you want to update?

>>:  Is there a bug in iOS 17.5? Users’ private photos reappear after being deleted!

Recommend

Marketing promotion hot calendar in May!

It always feels like I haven’t done anything yet,...

What are the specific methods of selling products on Douyin?

From Haidilao to Daancha, Douyin has demonstrated...

PERKINS COIE: Emerging Technology Trends Report 2022

PERKINS COIE released the "2022 Emerging Tec...

[Inspirational story] Don’t be afraid of falling and never give up

[Inspirational Story] Don't be afraid of falli...

Boiling tea around the fire, someone got infected

When the weather gets cold “Tea brewing around th...

What should I do if the price of Google UAC advertising suddenly increases?

When placing Google UAC ads , especially UAC1.0, ...

"Superkids Season 1" Baidu Cloud Download Link Address

Welcome to Aixue Knowledge Network, where you can...

Do cavities come from sweetness? No, they come from acidity | Graphic Science

We all know that eating too much sugar can cause ...

Android Basics: Toolbar Usage

Toolbar is introduced in Android 5.0 to replace t...

Who is the number one mobile phone in China?

Lenovo's own data shows that its domestic sma...