#include <binder/IServiceManager.h> #include <binder/IPCThreadState.h>
#include "Cal.h" #include "../libcalservice/CalService.h" /*包含CalService.h文件主要是要得到操作命令类型:ADD,SUB,MUL,DIV*/ namespace android { sp<IBinder> binder; int Cal::Add(int a, int b) { getCalService(); Parcel data, reply; data.writeInt32(getpid()); data.writeInt32(a); data.writeInt32(b); LOGD("BpCalService::create remote()->transact()\n"); binder->transact(CalService::ADD, data, &reply); int result = reply.readInt32(); return result; } int Cal::Sub(int a, int b) { getCalService(); Parcel data, reply; data.writeInt32(getpid()); data.writeInt32(a); data.writeInt32(b); LOGD("BpCalService::create remote()->transact()\n"); binder->transact(CalService::SUB, data, &reply); int result = reply.readInt32(); return result; } int Cal::Mul(int a, int b) { getCalService(); Parcel data, reply; data.writeInt32(getpid()); data.writeInt32(a); data.writeInt32(b); LOGD("BpCalService::create remote()->transact()\n"); binder->transact(CalService::MUL, data, &reply); int result = reply.readInt32(); return result; } float Cal::Div(int a, int b) { getCalService(); Parcel data, reply; data.writeInt32(getpid()); data.writeInt32(a); data.writeInt32(b); LOGD("BpCalService::create remote()->transact()\n"); binder->transact(CalService::DIV, data, &reply); float result = reply.readFloat(); return result; } /*获取CalService服务*/ const void Cal::getCalService() { sp<IServiceManager> sm = defaultServiceManager(); binder = sm->getService(String16("CalService")); //这里的服务名必须和注册服务时相同的服务名才可以得到相应服务 if(binder == 0 ) { LOGD("CalService not published, getCalService Fail"); return ; } } };
|