android binder机制实现
2010-05-31 14:30:00 来源:WEB开发网data.writeInt32(pid);
data.writeInt32(uid);
remote()->transact(CHECK_PERMISSION_TRANSACTION, data, &reply);
if (reply.readInt32() != 0) return 0;
return reply.readInt32() != 0;
}
};
class BpPermissionController : public BpInterface { public: BpPermissionController(const sp& impl) : BpInterface(impl) { } virtual bool checkPermission(const String16& permission, int32_t pid, int32_t uid) { Parcel data, reply; data.writeInterfaceToken(IPermissionController:: getInterfaceDescriptor()); data.writeString16(permission); data.writeInt32(pid); data.writeInt32(uid); remote()->transact(CHECK_PERMISSION_TRANSACTION, data, &reply); if (reply.readInt32() != 0) return 0; return reply.readInt32() != 0; } };
IMPLEMENT_META_INTERFACE(PermissionController, "android.os.IPermissionController");
BpPermissionController继承了BpInterface,它本身是一个
已经实现的类,而且并没有在接口中体现。这个类按照格式写就可以,在实现checkPermission()函数的过程中,使用Parcel作为传输数据
的容器,传输中时候transact()函数,其参数需要包含枚举值CHECK_PERMISSION_TRANSACTION。
IMPLEMENT_META_INTERFACE用于扶助生成。
BnPermissionController中实现的onTransact()函数如下所示:
Java代码 < width="14" height="15" src="http://mintelong.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" pluginspage="http://www.macromedia.com/go/getflashplayer">
status_t BnPermissionController:: BnTransact(
uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
{
switch(code) {
case CHECK_PERMISSION_TRANSACTION: {
CHECK_INTERFACE(IPermissionController, data, reply);
String16 permission = data.readString16();
int32_t pid = data.readInt32();
int32_t uid = data.readInt32();
bool res = checkPermission(permission, pid, uid);
赞助商链接