Android ServiceManager

一、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 is not started yet.
String[] getDeclaredInstances(@utf8InCpp String iface):Returns all declared instances for a particular interface.
String updatableViaApex(@utf8InCpp String name):If updatable-via-apex, returns the APEX via which this is updated.
String[] getUpdatableNames(@utf8InCpp String apexName):Returns all instances which are updatable via the APEX.
ConnectionInfo getConnectionInfo(@utf8InCpp String name):If connection info is available for the given instance, returns the ConnectionInfo
void registerClientCallback(@utf8InCpp String name, IBinder service, IClientCallback callback):Request a callback when the number of clients of the service changes.
void tryUnregisterService(@utf8InCpp String name, IBinder service):Attempt to unregister and remove a service. Will fail if the service is still in use.
ServiceDebugInfo[] getServiceDebugInfo():Get debug information for all currently registered services.

ServiceManager

ServiceManager是Android系统中的一个重要组件,它负责管理系统中的各种服务。ServiceManager提供了服务的注册、查询和启动等功能。

ServiceManager文件路径:

frameworks\base\core\java\android\os\ServiceManager.java

ServiceManager定义:

public final class ServiceManager {
    public static class ServiceNotFoundException extends Exception {}
}

ServiceManager方法:

public ServiceManager():构造方法
public static IBinder getService(String name):Returns a reference to a service with the given name.
public static IBinder getServiceOrThrow(String name):Returns a reference to a service with the given name, or throws {@link ServiceNotFoundException} if none is found.
public static void addService(String name, IBinder service):Place a new @a service called @a name into the service manager.
public static void addService(String name, IBinder service, boolean allowIsolated):Place a new @a service called @a name into the service manager.
public static void addService(String name, IBinder service, boolean allowIsolated, int dumpPriority):Place a new @a service called @a name into the service manager.
public static IBinder checkService(String name):Retrieve an existing service called @a name from the service manager.  
public static boolean isDeclared(@NonNull String name) :Returns whether the specified service is declared.
public static String[] getDeclaredInstances(@NonNull String iface):Returns an array of all declared instances for a particular interface.
public static IBinder waitForService(@NonNull String name):Returns the specified service from the service manager.
public static IBinder waitForDeclaredService(@NonNull String name):Returns the specified service from the service manager, if declared.
public static void registerForNotifications(@NonNull String name, @NonNull IServiceCallback callback):Register callback for service registration notifications.
public static ServiceDebugInfo[] getServiceDebugInfo():Get service debug info.
public static void initServiceCache(Map<String, IBinder> cache):This is only intended to be called when the process is first being brought up and bound by the activity manager. 

ServiceManagerNative

服务管理器的本机实现,内部类ServiceManagerProxy实现了IServiceManager 接口。

ServiceManagerNative文件路径:

frameworks\base\core\java\android\os\ServiceManagerNative.java

ServiceManagerNative定义:

public final class ServiceManagerNative {
    class ServiceManagerProxy implements IServiceManager {}
}

ServiceManagerProxy方法:

public ServiceManagerProxy(IBinder remote):构造方法
public IBinder asBinder()
public IBinder getService(String name) 
public IBinder checkService(String name) 
public void addService(String name, IBinder service, boolean allowIsolated, int dumpPriority)
public String[] listServices(int dumpPriority) 
public void registerForNotifications(String name, IServiceCallback cb)
public void unregisterForNotifications(String name, IServiceCallback cb)
public boolean isDeclared(String name) 
public String[] getDeclaredInstances(String iface) 
public String updatableViaApex(String name) 
public String[] getUpdatableNames(String apexName) 
public ConnectionInfo getConnectionInfo(String name)
public void registerClientCallback(String name, IBinder service, IClientCallback cb)
public void tryUnregisterService(String name, IBinder service) 
public ServiceDebugInfo[] getServiceDebugInfo()

IServiceManager

Service manager for C++ services.

IServiceManager文件路径:

frameworks/native/libs/binder/include/binder/IServiceManager.h

frameworks/native/libs/binder/IServiceManager.cpp

IServiceManager定义:

class IServiceManager : public IInterface{}
class ServiceManagerShim : public IServiceManager {}
class ServiceManagerHostShim : public ServiceManagerShim {}

IServiceManager方法:

virtual sp<IBinder> getService(const String16& name):Retrieve an existing service, blocking for a few seconds if it doesn't yet exist.
virtual sp<IBinder> checkService( const String16& name):Retrieve an existing service, non-blocking.
virtual status_t addService(const String16& name, const sp<IBinder>& service, bool allowIsolated = false, int dumpsysFlags = DUMP_FLAG_PRIORITY_DEFAULT):Register a service.
virtual Vector<String16> listServices(int dumpsysFlags = DUMP_FLAG_PRIORITY_ALL) :Return list of all existing services.
virtual sp<IBinder> waitForService(const String16& name) :Efficiently wait for a service.
virtual bool isDeclared(const String16& name):Check if a service is declared (e.g. VINTF manifest).
virtual Vector<String16> getDeclaredInstances(const String16& interface):Get all instances of a service as declared in the VINTF manifest
virtual std::optional<String16> updatableViaApex(const String16& name):If this instance is updatable via an APEX, returns the APEX with which this can be updated.
virtual Vector<String16> getUpdatableNames(const String16& apexName):Returns all instances which are updatable via the APEX. Instance names are fully qualified like `pack.age.IFoo/default`.
void setDefaultServiceManager(const sp<IServiceManager>& sm):Directly set the default service manager. 

service_manager

service_manager文件路径:

frameworks/native/libs/binder/ndk/service_manager.c

service_manager函数:

binder_exception_t AServiceManager_addService(AIBinder* binder, const char* instance)
binder_exception_t AServiceManager_addServiceWithFlags(AIBinder* binder, const char* instance, const AServiceManager_AddServiceFlag flags) 
AIBinder* AServiceManager_checkService(const char* instance) 
AIBinder* AServiceManager_getService(const char* instance) 
binder_status_t AServiceManager_registerLazyService(AIBinder* binder, const char* instance) 
AIBinder* AServiceManager_waitForService(const char* instance) 
AServiceManager_registerForServiceNotifications(const char* instance, AServiceManager_onRegister onRegister, void* cookie)
void AServiceManager_NotificationRegistration_delete(AServiceManager_NotificationRegistration* notification)
bool AServiceManager_isDeclared(const char* instance) 
void AServiceManager_forEachDeclaredInstance(const char* interface, void* context, void (*callback)(const char*, void*)) 
bool AServiceManager_isUpdatableViaApex(const char* instance)
void AServiceManager_getUpdatableApexName(const char* instance, void* context, void (*callback)(const char*, void*))
void AServiceManager_forceLazyServicesPersist(bool persist)
void AServiceManager_setActiveServicesCallback(bool (*callback)(bool, void*), void* context)
bool AServiceManager_tryUnregister() 
void AServiceManager_reRegister()

三、ServiceManager相关流程分析

ServiceManager启动流程分析

Android ServiceManager启动流程分析-CSDN博客

  • 13
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值