Native Binder 实例

这篇博客详细介绍了Android Native Binder的实现,通过TestServer.cpp、TestClient.cpp、ITestService.cpp和Test.h四个关键文件的代码示例,揭示了Binder在Android系统中进行进程间通信的核心机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<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)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值