Framework中的AIDL(原)

来源:互联网 发布:淘宝怎么刷流量安全 编辑:程序博客网 时间:2024/05/21 15:08

       在上一节(应用层的AIDL调用)中我们介绍了应用层中的AIDL用法, 这一节中,我们来看一下系统Framework层中更为普通的AIDL用法。

        为了便于分析,我们挑选了ITelephonyRegistry这个SystemService进行分析。这个服务的主要作用就是对通话相关的事件进行监听,我们把重心放在AIDL的实现结构上,不去关注ITelephonyRegistry具体的实现。

一、AIDL文件相关

        先来看一下这个服务的AIDL文件:

[java] view plain copy
  1. @ITelephonyRegistry.aidl  
  2. interface ITelephonyRegistry {  
  3.     void listen(String pkg, IPhoneStateListener callback, int events, boolean notifyNow);  
  4.     void notifyCallState(int state, String incomingNumber);  
  5.     void notifyServiceState(in ServiceState state);  
  6.     void notifySignalStrength(in SignalStrength signalStrength);  
  7.     void notifyMessageWaitingChanged(boolean mwi);  
  8. }  
        再来看这个服务的真正实现:

[java] view plain copy
  1. @TelephonyRegistry.java  
  2. class TelephonyRegistry extends ITelephonyRegistry.Stub {  
  3.     TelephonyRegistry(Context context) {  
  4.         ......  
  5.     }  
  6.     void listen(String pkg, IPhoneStateListener callback, int events, boolean notifyNow){  
  7.         ......  
  8.     }  
  9.     void notifyCallState(int state, String incomingNumber){  
  10.         ......  
  11.     }  
  12.     void notifyServiceState(in ServiceState state){  
  13.         ......  
  14.     }  
  15.     void notifySignalStrength(in SignalStrength signalStrength){  
  16.         ......  
  17.     }  
  18.     void notifyMessageWaitingChanged(boolean mwi){  
  19.         ......  
  20.     }  
  21. }  

        上面的两个文件是这个服务的核心部分,aidl文件规定了这个服务的功能,而java文件是对功能的具体实现。但是,此时的TelephonyRegistry并没有继承Service的类,也就是说,当前他并不具备作为一个Service的资格。那么他是如何变成一个服务的呢?

二、服务的注册过程

        我们在SystemService中找到了答案。
[java] view plain copy
  1. @SystemServer.java  
  2. class ServerThread extends Thread {  
  3.     @Override  
  4.     public void run() {  
  5.         try {  
  6.             telephonyRegistry = new TelephonyRegistry(context);  
  7.             ServiceManager.addService("telephony.registry", telephonyRegistry);  
  8.         }  
  9.     }  
  10. }  
        我们看到,在这一步中,把telephonyRegistry对象(也就是ITelephonyRegistry.Stub的子类对象)作为一个Service注册给了ServiceManager。并且注册的名字是“telephony.registry”
        有了这一步,TelephonyRegistry就可以作为服务提供者向客户端开放了。也就是说,有了这一步,TelephonyRegistry才算上是一个真正的Service,可以接受客户端的连接申请

        那么接下来,我们怎么得到这个Service呢?

三、如何得到注册的服务

        既然通过ServiceManager注册了服务,我们就需要再次通过ServiceManager得到它的服务对象。
[java] view plain copy
  1. private ITelephonyRegistry sRegistry;  
  2. sRegistry = ITelephonyRegistry.Stub.asInterface(ServiceManager.getService("telephony.registry"));  

        通过这样的方法,我们就得到了ITelephonyRegistry.aidl的对象sRegistry。

四、流程总结

        我们现在回顾一下这种AIDL服务的框架。
        我们通过继承ITelephonyRegistry.Stub父类去实现AIDL中规定的接口,然后把TelephonyRegistry作为ServiceManager注册给SystemService。客户端可以通过ServiceManager得到TelephonyRegistry的对象,然后就可以去调用AIDL中定义的接口了。
1 0
原创粉丝点击