1.1.1 IInterface类
(1) 定义:
class IInterface : public virtual RefBase
{
public:
IInterface();
sp<IBinder> asBinder();
sp<const IBinder> asBinder() const;
protected:
virtual ~IInterface();
virtual IBinder* onAsBinder() = 0;
};
IInterface是RefBase的派生类,这意味着IInterface类或IInterface的派生类可以通过
模板类sp或wp对它们的对象进行引用计数,从而及时自动释放该对象.
#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();
#define IMPLEMENT_META_INTERFACE(INTERFACE, NAME) \
const android::String16 I##INTERFACE::descriptor(NAME); \
const android::String16& \
I##INTERFACE::getInterfaceDescriptor() const { \
return I##INTERFACE::descriptor; \
} \
android::sp<I##INTERFACE> I##INTERFACE::asInterface( \
const android::sp<android::IBinder>& obj) \
{ \
android::sp<I##INTERFACE> intr; \
if (obj != NULL) { \
intr = static_cast<I##INTERFACE*>( \
obj->queryLocalInterface( \
I##INTERFACE::descriptor).get()); \
if (intr == NULL) { \
intr = new Bp##INTERFACE(obj); \
} \
} \
return intr; \
} \
I##INTERFACE::I##INTERFACE() { } \
I##INTERFACE::~I##INTERFACE() { } \
宏DECLARE_META_INTERFACE和IMPLEMENT_META_INTERFACE,分别声明和定义了IXXX类(如ICamera),
asInterface()也是在此定义.
(2) 成员函数asBinder()定义:
sp<IBinder> IInterface::asBinder()
{
return this ? onAsBinder() : NULL;
}
asBinder()函数返回onAsBinder()函数的返回值,onAsBinder()是IIterface类的虚函数,在IIterface的子类BnIterface中定义.
1.1.2 模板类BnInterface
(1) 定义:
template<typename INTERFACE>
class BnInterface : public INTERFACE, public BBinder
{
public:
virtual sp<IInterface> queryLocalInterface(const String16& _descriptor);
virtual const String16& getInterfaceDescriptor() const;
protected:
virtual IBinder* onAsBinder();
};
参数INTERFACE是IInterface类的派生类,同时也是BnInterface的父类,onAsBinder()在IIterface的成员函数中被调用,我们看下其定义:
templat
(1) 定义:
class IInterface : public virtual RefBase
{
public:
IInterface();
sp<IBinder> asBinder();
sp<const IBinder> asBinder() const;
protected:
virtual ~IInterface();
virtual IBinder* onAsBinder() = 0;
};
IInterface是RefBase的派生类,这意味着IInterface类或IInterface的派生类可以通过
模板类sp或wp对它们的对象进行引用计数,从而及时自动释放该对象.
#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();
#define IMPLEMENT_META_INTERFACE(INTERFACE, NAME) \
const android::String16 I##INTERFACE::descriptor(NAME); \
const android::String16& \
I##INTERFACE::getInterfaceDescriptor() const { \
return I##INTERFACE::descriptor; \
} \
android::sp<I##INTERFACE> I##INTERFACE::asInterface( \
const android::sp<android::IBinder>& obj) \
{ \
android::sp<I##INTERFACE> intr; \
if (obj != NULL) { \
intr = static_cast<I##INTERFACE*>( \
obj->queryLocalInterface( \
I##INTERFACE::descriptor).get()); \
if (intr == NULL) { \
intr = new Bp##INTERFACE(obj); \
} \
} \
return intr; \
} \
I##INTERFACE::I##INTERFACE() { } \
I##INTERFACE::~I##INTERFACE() { } \
宏DECLARE_META_INTERFACE和IMPLEMENT_META_INTERFACE,分别声明和定义了IXXX类(如ICamera),
asInterface()也是在此定义.
(2) 成员函数asBinder()定义:
sp<IBinder> IInterface::asBinder()
{
return this ? onAsBinder() : NULL;
}
asBinder()函数返回onAsBinder()函数的返回值,onAsBinder()是IIterface类的虚函数,在IIterface的子类BnIterface中定义.
1.1.2 模板类BnInterface
(1) 定义:
template<typename INTERFACE>
class BnInterface : public INTERFACE, public BBinder
{
public:
virtual sp<IInterface> queryLocalInterface(const String16& _descriptor);
virtual const String16& getInterfaceDescriptor() const;
protected:
virtual IBinder* onAsBinder();
};
参数INTERFACE是IInterface类的派生类,同时也是BnInterface的父类,onAsBinder()在IIterface的成员函数中被调用,我们看下其定义:
templat

本文详细介绍了Android中Binder通信机制的关键类和成员函数,包括IInterface类的声明与实现,BnInterface模板类,BpInterface模板类以及它们的onAsBinder()函数。此外,还讲解了BpRefBase类的构造函数和remote()函数,以及BpBinder类的transact()函数的工作原理。通过对这些核心组件的解析,深入理解Binder的交互过程。
最低0.47元/天 解锁文章
1051

被折叠的 条评论
为什么被折叠?



