简体   繁体   English

Android如何精确地链接AIDL接口和远程服务?

[英]How exactly Android links AIDL interface and Remote Service?

To avoid useless answer about how to implement AIDL in your own project - I understand how to use AIDL in your own project. 为避免对如何在自己的项目中实现AIDL做出无用的回答-我了解如何在自己的项目中使用AIDL。

What's unclear for me is how it is used in Android source code? 我不清楚在Android源代码中如何使用它?

For example, there is IAudioService AIDL interface in android.media package. 例如,android.media包中有IAudioService AIDL接口。 And there is AudioService java file. 并且有AudioService java文件。 And there are a lot of examples where interface is defined in IInterfaceServiceName.aidl and implementation is in InterfaceServiceName.java. 并且有很多示例,其中接口在IInterfaceServiceName.aidl中定义,而实现在InterfaceServiceName.java中。 So, I thought that internals of AIDL look for Service with the name without first capital I, and then use it. 因此,我认为AIDL的内部人员会寻找名称不带首字母I的Service,然后再使用它。 However, this theory was demolished by IWifiManager and WifiService. 但是,该理论已被IWifiManager和WifiService拆除。

All of this AIDL implementations, however, share one thing - they all extend IInterfaceName.Stub class. 但是,所有这些AIDL实现都共享一件事-它们都扩展了IInterfaceName.Stub类。 So, that means AIDL internals look for classes extending IInterfaceName.Stub? 因此,这意味着AIDL内部人员会寻找扩展IInterfaceName.Stub的类吗? But what if there are multiple classes extending it? 但是,如果有多个扩展类呢? And those classes aren't services at all... 这些类根本不是服务...

Or there is some Service Manager, which somehow creates Services with those AIDL implementations as Binders? 还是有一些服务管理器,以某种方式将那些AIDL实现作为绑定来创建服务? If so, please direct me to this mysterious service manager. 如果是这样,请将我定向到这位神秘的服务经理。

EDIT: I've found this service manager. 编辑:我已经找到了这个服务经理。 It is ServiceManager in package android.os. 它是android.os包中的ServiceManager。 Now it's understandable why those SomethingService aren't actually services but are actually IBinders - the service manager somehow creates services with given IBinder by method add service(String, IBinder). 现在可以理解为什么SomethingService并不是真正的服务而是IBinder了-服务管理器以某种方式通过给定的IBinder通过方法add service(String,IBinder)创建服务。 But now confusion only becomes even worse. 但是现在混乱只会变得更加严重。 The implementation of this service also lies in somewhat strange ServiceManagerNative, which involves remote transaction with unknown IBinder. 该服务的实现还存在于某种奇怪的ServiceManagerNative中,它涉及与未知IBinder的远程事务。 Now I'm totally lost. 现在我完全迷路了。 How then does this ServiceManager starts itself up? 然后,该ServiceManager如何启动? How are the services added by it? 如何添加服务?

ServiceManager has not a random but a predefined value. ServiceManager不是随机值,而是预定义值。 It is a Service with Binder handle equal to 0. During the boot of the system ServiceManager is one of the first services that are started by the system (you can check this in init.rc file). 它是具有等于0的活页夹句柄的服务。在系统引导过程中,ServiceManager是系统启动的第一批服务之一(您可以在init.rc文件中进行检查)。 Binder permits only one service to act as a name resolver. 活页夹仅允许一项服务充当名称解析器。 All other services started in the system are registered with ServiceManager. 系统中启动的所有其他服务都已向ServiceManager注册。

IAudioService.aidl defines public methods that are visible for other processes. IAudioService.aidl定义了对其他进程可见的公共方法。 During compilation, from .aidl there is an interface IAudioService generated in which you can find Stub class: 在编译期间,从.aidl生成一个接口IAudioService,您可以在其中找到Stub类:

public static abstract class Stub extends android.os.Binder implements android.media.IAudioService{

now the class AudioService extends this Stub 现在,类AudioService扩展了此Stub

public class AudioService extends IAudioService.Stub implements OnFinished {

locations for all mentioned files, from main android folder: 所有提到的文件的位置,位于android主文件夹中:

./frameworks/base/media/java/android/media/IAudioService.aidl
./out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/src/media/java/android/media/IAudioService.java
./frameworks/base/media/java/android/media/AudioService.java

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM