在 Framework 中使用 AIDL 服务,其发布和获取是两个重要方法,本文尝试探究其源码的实现。
我们知道手机在开机过程中,会启动一个 ServiceManger 的服务,它就是服务的大管家。AIDL 服务的发布和获取也是要通过这个大管家来操作的,下面是大管家的路径。
/frameworks/native/cmds/servicemanager/ServiceManager.cpp
1 AIDL 的发布方法
publishBinderService
该方法定义在 SystemService.java 中,有多个重载方法,最后都调到了下面这个方法。
/frameworks/base/services/core/java/com/android/server/SystemService.java
429 /**
430 * Publish the service so it is accessible to other services and apps.
431 *
432 * @param name the name of the new service
433 * @param service the service object
434 * @param allowIsolated set to true to allow isolated sandboxed processes
435 * to access this service
436 * @param dumpPriority supported dump priority levels as a bitmask
437 *
438 * @hide
439 */
440 protected final void publishBinderService(String name, IBinder service,
441 boolean allowIsolated, int dumpPriority) {
442 ServiceManager.addService(name, service, allowIsolated, dumpPriority);
443 }
进一步调到 ServiceManager.addService
,这里的 ServiceManager 还是 java 层的。我们继续看看它内部的实现
194 public static void addService(String name, IBinder service, boolean allowIsolated,
195 int dumpPriority) {
196 try {
197 getIServiceManager().addService(name, service, allowIsolated, dumpPriority);
198 } catch (RemoteException e) {