Android中实现native服务利用binder与应用通信

来源:互联网 发布:unity3d仙侠资源 编辑:程序博客网 时间:2024/05/16 07:59

主要讲解一下,Android的上层应用通过binder机制调用native服务,下面这个图,主要用来描述,我们在实现binder相关的bp和bn端的时候,基本的类图。



上图基本说明了一下 如何利用binder实现底层的服务,基本实现就是上面这样一个模板,下面我会就我自己实现的一个demo,详细说明一下,如何来写code,之前也是看别人的博客,参考到的例子,但是例子在我这有很多问题,就改了改,反正是可以正常使用了。



上图是基本说明了,我的这个demo的类继承关系,这个demo主要实现了Java上层利用binder调用C++层的bn端,而后C++的bn端再将请求返回给上层的,这样一个实例,这个实例主要是做了一个加法运算。


C++实现的native服务


AddService.h

#ifndef ANDROID_GUILH_ADD_SERVICE_H#define ANDROID_GUILH_ADD_SERVICE_H#include <utils/threads.h>#include <utils/RefBase.h>#include <binder/IInterface.h>#include <binder/Parcel.h>#include <binder/IBinder.h>#include <binder/Binder.h>#include <binder/IBinder.h>#include <binder/IServiceManager.h>#include <utils/Log.h>#include <utils/misc.h>#include <binder/Parcel.h>#include <utils/StringArray.h>#include <utils/threads.h>#include <cutils/properties.h>#include "jni.h"#include "JNIHelp.h"#include <stdio.h>#include <signal.h>#include <sys/stat.h>#include <sys/types.h>#include <signal.h>#include <dirent.h>#include <assert.h>namespace android{class AddService : public BBinder{mutable Mutex mLock;int32_t mNextConnId;public:static int instantiate();AddService();virtual ~AddService();virtual status_t onTransact(uint32_t, const Parcel&, Parcel*, uint32_t);};}#endif


AddService.cpp

#include <AddService.h>#include <binder/IServiceManager.h>#include <utils/Debug.h>#include <utils/Log.h>#include <binder/IPCThreadState.h>#include <binder/Parcel.h>//#include <binder/IBinder.h>#include <utils/String8.h>#include <binder/IBinder.h>#include <binder/IServiceManager.h>#include <utils/Log.h>#include <utils/misc.h>#include <binder/Parcel.h>#include <utils/StringArray.h>#include <utils/threads.h>#include <cutils/properties.h>#include "jni.h"#include "JNIHelp.h"//#include "android_util_Binder.h"#include <stdio.h>#include <signal.h>#include <sys/stat.h>#include <sys/types.h>#include <signal.h>#include <dirent.h>#include <assert.h>namespace android {static struct sigaction oldact;static pthread_key_t sigbuskey;sp<IBinder> binder;int AddService::instantiate() {LOGE("AddService instantiate");int r = defaultServiceManager()->addService(String16("flyfot.add"), new AddService());LOGE("AddService r = %d/n", r);return r;}AddService::AddService(){LOGV("AddService created");mNextConnId = 1;pthread_key_create(&sigbuskey, NULL);}AddService::~AddService(){pthread_key_delete(sigbuskey);LOGV("AddService destroyed");}status_t AddService::onTransact(uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {LOGE("AddDemo", "AddService.cpp:onTransact:" + code);switch (code) {case 0: {LOGI("AddDemo", "addservice::onTransact");//pid_t pid = data.readInt32();//int a = data.readInt32();//int b = data.readInt32();//int num = a + b;//sp < IServiceManager > sm = defaultServiceManager();LOGE("AddService","start get java service");//binder = sm->getService(//String16("zx.java"));//String16 info = data.re//data.enbinder = data.readStrongBinder();LOGE("AddService.cpp","data.readStrongBinbder");if (binder == 0) {LOGE("AddService", "get java local service failed");return NO_ERROR;}int a = data.readInt32();int b = data.readInt32();Parcel _data_sec ;//String16 interface = "cn.zx.callback";_data_sec.writeInterfaceToken(String16("cn.zx.secrity.RemoteSecrityCallback"));_data_sec.writeInt32(a);_data_sec.writeInt32(b);LOGE("AddService", "get java local service sucessful");binder->transact(0,_data_sec, reply);return NO_ERROR;}break;default:return BBinder::onTransact(code, data, reply, flags);}}};


。。。。。待续/。。。。。