Native Binder 实例

来源:互联网 发布:手机c语言编辑器 编辑:程序博客网 时间:2024/05/22 16:44

<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_tonTransact(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    := TestServerLOCAL_SRC_FILES := \    TestServer.cpp \    ITestService.cpp LOCAL_MODULE_TAGS := optionalinclude $(BUILD_EXECUTABLE) #生成binder service的测试client端include $(CLEAR_VARS)LOCAL_SHARED_LIBRARIES := \    libcutils \    libutils \    libbinder LOCAL_MODULE    := TestClientLOCAL_SRC_FILES := \    TestClient.cpp \    ITestService.cpp LOCAL_MODULE_TAGS := optionalinclude $(BUILD_EXECUTABLE)


0 0