BpBinder对象如何转换为getService取得的对象的?

来源:互联网 发布:黎明教务网络管理系统 编辑:程序博客网 时间:2024/04/30 23:14
#define DECLARE_META_INTERFACE(INTERFACE)                               \
    static const android::String16 descriptor;                          \
    static android::sp<I##INTERFACE> asInterface(                       \
            const android::sp<android::IBinder>& obj);                  \
    virtual const android::String16& getInterfaceDescriptor() const;    \
    I##INTERFACE();                                                     \

    virtual ~I##INTERFACE();                                            \

假定我们的service名称是IMyService。那么以上定义可以转换成如下:


#define DECLARE_META_INTERFACE(MyService)                               
    static const android::String16 descriptor;                   

    static android::sp<IMyService> asInterface(const android::sp<android::IBinder>& obj); 

    virtual const android::String16& getInterfaceDescriptor() const;    

    IMyService();                                                     

    virtual ~IMyService();    

#define IMPLEMENT_META_INTERFACE(MyService, my.package)                       
    const android::String16 IMyService::descriptor(my.package.Myservice);             


    const android::String16&   IMyService::getInterfaceDescriptor() const {              
        return  IMyService::descriptor;                                
    }                                                                   
    android::sp<IMyService>IMyService::asInterface(  const android::sp<android::IBinder>& obj)                   
    {                                                                   
        android::sp<IMyService> intr;                                 
        if (obj != NULL) {                                              
            intr = static_cast<IMyService*>(    obj->queryLocalInterface(  IMyService::descriptor).get());    这里返回的都是空, BpBinder.cpp文件里面           
            if (intr == NULL) {                                         
                intr = new BpMyService(obj);                          
            }                                                           
        }                                                               
        return intr;                                                    
    }                                                                   
   IMyService::IMyService() { }                                    
    IMyService::~IMyService() { }           


asInterface传入的对象是BpBinder对象。

sp<IServiceManager> sm = defaultServiceManager();
  sp<IBinder> binder;
        
            binder = sm->getService(String16("myService"));

 mMyService = interface_cast<IMyService>(binder);


interface_cast调用的就是asInterface。


原创粉丝点击