一、ServiceManager介绍
ServiceManager是Binder的守护进程,在Android上如果service manager挂掉,所有采用Binder通信的进程服务都会挂掉。
ServiceManager本身也是一个Binder服务,handle固定为0。提供注册服务,查询服务的功能。应用程序相要通过Binder向一个service发送数据,必须先通过Service Manager获取该service的handle然后才能通过binder驱动与service通信。
Service Manager既然也是一个service,那按照流程,Service Manager的逻辑流程就应该如下:
1.打开binder驱动
2.向binder驱动注册一个service,这个service就是servicemanager
3.进入一个loop循环,轮询binder驱动,查看是否有其他的进行要与service manager通信(注册service 查询service等)
ServiceManager于android binder来说是非常重要的一部分。ServiceManager在java层与native层都有各自的逻辑,整体的流程图:
二、ServiceManager相关类、接口、文件
IServiceManager
Basic interface for finding and publishing system services.
IServiceManager文件路径:
frameworks/native/libs/binder/aidl/android/os/IServiceManager.aidl
IServiceManager定义:
interface IServiceManager {}
IServiceManager方法:
IBinder getService(@utf8InCpp String name):Retrieve an existing service called @a name from the service manager.
IBinder checkService(@utf8InCpp String name):Retrieve an existing service called @a name from the service manager.
void addService(@utf8InCpp String name, IBinder service, boolean allowIsolated, int dumpPriority):Place a new @a service called @a name into the service manager.
String[] listServices(int dumpPriority):Return a list of all currently running services.
void registerForNotifications(@utf8InCpp String name, IServiceCallback callback):Request a callback when a service is registered.
void unregisterForNotifications(@utf8InCpp String name, IServiceCallback callback):Unregisters all requests for notifications for a specific callback.
boolean isDeclared(@utf8InCpp String name):Returns whether a given interface is declared on the device, even if it