android binder

本文介绍如何使用 Android 的 native 层 (C++) 实现一个基于 Binder 机制的服务。包括定义接口、实现服务端与客户端交互的过程。具体展示了 TestService 的实现步骤。

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


android native(C++)层的service 一般都会用到binder,通过binder的机制对外提供接口,如果我们需要实现一个native 层的service,那么要怎么做呢?

这个基本上是一个固定的套路,所以我们需要先了解一下android 提供的binder (框架?)的关系。XXXService就是要基于binder通信的类。



    例如要实现一个TestService

    1、ITestService.h 

      

#ifndef ANDROID_HXIONG_ITEST_H
#define ANDROID_HXIONG_ITEST_H

#include <stdint.h>
#include <sys/types.h>
#include <utils/Errors.h>
#include <utils/RefBase.h>

#include <binder/IInterface.h>

namespace android {

class ITestService : public IInterface
{
public:
    DECLARE_META_INTERFACE(ITestService);
    virtual status_t test(int api) = 0;
};

// ----------------------------------------------------------------------------

class BnTestService : public BnInterface<ITestService>
{
public:
    virtual status_t    onTransact( uint32_t code,
                                    const Parcel& data,
                                    Parcel* reply,
                                    uint32_t flags = 0);
};

// ----------------------------------------------------------------------------
}; // namespace android

#endif 

     2、ITestService.cpp

#include <binder/Parcel.h>
#include <binder/IInterface.h>
#include <ITestService.h>

namespace android {
// ----------------------------------------------------------------------------

enum {
    TEST = IBinder::FIRST_CALL_TRANSACTION,
}

class BpTestService : public BpInterface<ITestService>
{

    BpTestService()
    {
    }
    virtual ~BpTestService();
    
    virtual status_t test(int api) {
        Parcel data, reply;
        data.writeInterfaceToken(ITestService::getInterfaceDescriptor());
        data.writeInt32(api);
        remote()->transact(TEST, data, &reply);
        result = reply.readInt32();
        return result;
    }
};
IMPLEMENT_META_INTERFACE(TestService, "com.hxiong.ITestService");

status_t BnTestService::onTransact(
    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
{
    switch(code) {
        case TEST: {
            CHECK_INTERFACE(ITestService, data, reply);
            int api = data.readInt32();
            int result=test(api);    //这里会调用到TestService 的test函数
            reply->writeInt32(result);
            return NO_ERROR;
        }
    }
    return BBinder::onTransact(code, data, reply, flags);
}

     3、TestService.h

#ifndef ANDROID_HXIONFG_TEST_H
#define ANDROID_HXIONFG_TEST_H

#include <ITestService.h>

namespace android {


class TestService : public BnTestService{
public:
  
    TestService();
    virtual ~TestService();
  
     virtual status_t test(int api);
};
}
#endif

4、TestService.cpp

#include <TestService.h>

namespace android {

	TestService::TestService(){}

	TestService::~TestService() {}

	status_t TestService::test(int api){
	   return 0;
	}

}


    



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值