2

Android系统的心脏,SystemServer进程及各系统服务之间的关系

 2 months ago
source link: https://www.51cto.com/article/788394.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

SystemServer

SystemServer是Android系统中的一个重要进程,是zygote fork的第一个进程,负责启动和管理系统中的各种服务。在Android系统中,SystemServer进程的名称为"system_server"。

SystemServer进程启动后,会加载SystemServer类并执行其main函数,main函数是SystemServer的入口点,负责启动和初始化各种系统服务。在这个过程中,SystemServer会创建一个Looper和一个Handler,用于在主线程中处理消息和运行任务。Looper是Android事件循环的一部分,负责在主线程中接收和分发消息,Handler则用于发送和处理消息。

SystemServer创建并初始化的系统服务包括ActivityManagerService(AMS)、PackageManagerService(PMS)、WindowManagerService(WMS)等。这些服务在Android系统中扮演着重要的角色,例如AMS负责管理应用程序的生命周期和活动状态,PMS负责管理应用程序包的安装和卸载,WMS则负责管理窗口的创建、显示和更新等。

在SystemServer的main函数中,还会启动其他各种系统服务,这些服务通过SystemServiceManager进行管理和控制。SystemServer进程会一直运行,处理来自各种系统服务的消息,确保系统的正常运行和稳定性。

ServiceManager

ServiceManager是Android系统中的一个重要守护进程,负责管理系统服务的注册、查找和启动。

  1. 「作用」:

提供服务注册和查找:ServiceManager充当了一个中央注册表的角色,允许应用程序和系统组件将自己注册为服务,并提供一个唯一的服务名称。其他应用程序可以通过ServiceManager查找并获取已注册的服务,从而实现进程间通信。

启动和管理系统进程:ServiceManager还负责启动和管理一些重要的系统进程,例如系统服务(如Telephony服务、Media服务等),以及其他一些重要的系统组件。

实现Binder机制:Android系统采用Binder作为进程间通信(IPC)的机制,ServiceManager是Binder通信的关键组件之一。

  1. 「权限」:

ServiceManager的访问权限较高,一般只有系统级应用或者具有系统权限的应用程序才能够使用ServiceManager进行服务的注册和查询。

  1. 「与Binder的关系」:

ServiceManager是Binder的守护进程,在Android上如果ServiceManager挂掉,所有采用Binder通信的进程服务都会受到影响。ServiceManager本身也是一个Binder服务,其handle固定为0。应用程序相要通过Binder向一个service发送数据,必须先通过ServiceManager获取该service的handle,然后才能通过binder驱动与service通信。

  1. 「启动流程」:

在Android系统的启动过程中,SystemServer进程在启动时会启动ServiceManager,并将各种系统服务注册到ServiceManager中。

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

SystemServiceManager

SystemServiceManager是Android系统中用于管理系统服务的一个类。负责管理所有注册的系统级别服务,并在需要时启动和停止它们。例如,当SystemServer进程启动时,SystemServiceManager会注册PackageManagerService、WindowManagerService、ActivityManagerService等服务,并在需要时启动。

@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通过调用registerService函数来注册系统服务,并可以通过startService、stopService函数来启动或停止这些服务。当系统不再需要某个服务时,SystemServiceManager也会负责将其停止并卸载。

SystemServiceManager是Android系统内部使用的组件,通常不需要开发者直接与其交互。

SystemService

SystemService是framework的一些对应功能的服务,供其他模块和APP来调用。这些服务通常与特定的功能或模块相关,例如BatteryService(用于获取电池属性、充电状态、百分比等)、PowerManagerService(控制休眠、唤醒等)以及TvInputManagerService(用于创建和释放会话等)。

SystemService的使用相对简单,通过context.getSystemService(@NonNull Class<T> serviceClass)或Object getSystemService(@ServiceName @NonNull String name)方法获取一个manager对象,然后调用SystemService里面的方法。

SystemService是Android系统内部使用的组件,开发者在开发Android应用程序时,通常是通过系统提供的API来与系统服务进行交互的,而不是直接操作SystemService。

val powerManager = getSystemService(Context.POWER_SERVICE) as PowerManager

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK