此文章主要目的:在Android 6.0 arm 64位环境下使用C++编程实现binder通讯服务端和客户端
即native层binder服务通讯实现
此例子同时实现了int和string数据通讯接口
程序框架图
1.在Android源码目录下存放代码树形结构
development目录下创建相关文件列表
jack_server/
├── Android.mk
├── log.h
├── service
│ ├── Android.mk
│ ├── JACK_Service.cpp
│ └── JACK_Service.h
├── svcclient
│ ├── Android.mk
│ ├── JACK_Client.cpp
│ └── JACK_Client.h
├── svcserver
│ ├── Android.mk
│ └── JACK_server.cpp
└── svctest
├── Android.mk
└── test.cpp
2.首先实现服务端
创建目录service 再创建如下三个文件
JACK_Service.h
#ifndef ANDROID_JACK_SERVICE_H
#define ANDROID_JACK_SERVICE_H
#include <utils/RefBase.h>
#include <binder/IInterface.h>
#include <binder/Parcel.h>
namespace android
{
class JACK_Service : public BBinder
{
private:
//mutable Mutex m_Lock;
//int32_t m_NextConnId;
public:
static int Instance();
JACK_Service();
virtual ~JACK_Service();
virtual status_t onTransact(uint32_t, const Parcel&, Parcel*, uint32_t);
};
}
#endif
JACK_Service.cpp
#include <binder/IServiceManager.h>
#include <binder/IPCThreadState.h>
#include "JACK_Service.h"
#include "../log.h"
namespace android
{
//static struct sigaction oldact;
static pthread_key_t sigbuskey;
int JACK_Service::Instance()
{
DebugPrint("JACK_Service Instantiate\n");
int ret = defaultServiceManager()->addService(
String16("jack.svc"), new JACK_Service());
DebugPrint("