您的位置:首页 > 移动开发 > Android开发

Native Binder 实例

2014-10-08 11:04 363 查看
<span style="font-family: Arial, Helvetica, sans-serif;">该实例是NATIVE实例,在ANDROID系统编译后可直接通信</span>


主要包括四个文件:TestServer.cpp,  TestClient.cpp   ITestService.cpp  Test.h

具体代码如下:

TestServer.cpp

#include "Test.h"
namespace android {
class BnTestService: public BnInterface<ITestService> {
public:
virtual status_t
onTransact(uint32_t code, const Parcel& data, Parcel* reply,
uint32_t flags = 0);
virtual void test() {
printf("Now get test\n");
}

};
status_t BnTestService::onTransact(uint_t code, const Parcel& data,
Parcel* reply, uint32_t flags) {
switch (code) {
case TEST: {
printf("got the client msg\n");
CHECK_INTERFACE(ITest, data, reply);
test();
reply->writeInt32(100);
return NO_ERROR;
}
break;
case ZZTEST:
printf("ZZTEST got the client msg\n");
CHECK_INTERFACE(ITest, data, reply);
reply->writeInt32(100);
break;
default:
break;
}
return NO_ERROR;
}
}
int main() {
sp < ProcessState > proc(ProcessState::self());
sp < IServiceManager > sm = defaultServiceManager();
sm->addService(String16("service.testservice"), new BnTestService());
ProcessState::self()->startThreadPool();
IPCThreadState::self()->joinThreadPool();
return 0;
}


TestClient.cpp

#include <Test.h>
namespace android{

BpTestService::BpTestService(const sp < IBinder > & impl):
BpInterface<ITestService>(impl){
}

void BpTestService::test()
{
printf("in the get Test\n");
Parcel data, reply;
data.writeInterfaceToken(ITestService::getInterfaceDescriptor());
remote()->transact(TEST,data, &reply);
printf("send Print %d\n", reply.readInt32());
}
}

int main (void)
{
sp <IServiceManager> sm = defaultServiceManager();
sp <IBinder> binder = sm->getService(String16("service.testservice"));
sp <ITestService> cs = interface_cast <ITestService> (binder);
cs->test();
return 0;
}


ITestService.cpp

#include "Test.h"
namespace android
{
IMPLEMENT_META_INTERFACE(TestService, "android.TestServer.ITestService");
}


Test.h

#ifndef TEST_H_H
#define TEST_H_H
#include <stdio.h>
#include <binder/IInterface.h>
#include <binder/Parcel.h>
#include <binder/IBinder.h>
#include <binder/Binder.h>
#include <binder/ProcessState.h>
#include <binder/IPCThreadState.h>
#include <binder/IServiceManager.h>
using namespace android;
namespace android
{
class ITestService : public IInterface
{
public:
DECLARE_META_INTERFACE(TestService); // declare macro
virtual void test()=0;
};

enum
{
TEST = IBinder::FIRST_CALL_TRANSACTION,
ZZTEST,
};

class BpTestService: public BpInterface<ITestService> {
public:
BpTestService(const sp<IBinder>& impl);
virtual void test();
};
}
#endif

Android.mk

LOCAL_PATH := $(call my-dir)
#生成binder service的服务端
include $(CLEAR_VARS)
LOCAL_SHARED_LIBRARIES := \
libcutils \
libutils \
libbinder
LOCAL_MODULE := TestServer
LOCAL_SRC_FILES := \
TestServer.cpp \
ITestService.cpp
LOCAL_MODULE_TAGS := optional
include $(BUILD_EXECUTABLE)

#生成binder service的测试client端
include $(CLEAR_VARS)
LOCAL_SHARED_LIBRARIES := \
libcutils \
libutils \
libbinder
LOCAL_MODULE := TestClient
LOCAL_SRC_FILES := \
TestClient.cpp \
ITestService.cpp
LOCAL_MODULE_TAGS := optional
include $(BUILD_EXECUTABLE)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android binder native