Android Binder(C++版本例子)

packages/apps/Godv/IGodvService.h

//
// Created by godv on 21-6-29.
//  这个是接口
// 参考  frameworks/av/include/media/IMediaPlayerService.h

#ifndef ANDROID_IGODVSERVICE_H
#define ANDROID_IGODVSERVICE_H

#include <utils/Errors.h>  // for status_t
#include <utils/KeyedVector.h>
#include <utils/RefBase.h>
#include <utils/String8.h>
#include <binder/IInterface.h>
#include <binder/Parcel.h>

#define HELLO_SVR_CMD_SAYHELLO 0
#define HELLO_SVR_CMD_SAYHELLO_TO 1

namespace android {
    class IGodvService: public IInterface
    {
    public:
        //声明一些必须的函数
        DECLARE_META_INTERFACE(GodvService);
        //纯虚函数
        virtual void sayhello(void) = 0;
        virtual int sayhello_to(const char *name) = 0;

    };

    //声明Bn
    //BnInterface 模板类
    //相当于 BnGodvService : IGodvService, BBinder
    class BnGodvService: public BnInterface<IGodvService>
    {
    public:
        virtual status_t    onTransact( uint32_t code,
                                        const Parcel& data,
                                        Parcel* reply,
                                        uint32_t flags = 0);

        virtual void sayhello(void);
        virtual int sayhello_to(const char *name);
    };
};

#endif //ANDROID_IGODVSERVICE_H

packages/apps/Godv/BnGodvService.cpp

//
// Created by godv on 21-6-29.
// Bn Binder native 是服务端使用的 接收,解析,调用,回复类
// 参考  frameworks/av/media/libmedia/IMediaPlayerService.cpp
#define LOG_TAG "GodvService"

#include "IGodvService.h"

namespace android {

    //onTransact  用来解析数据 调用函数
    status_t BnGodvService::onTransact(
        uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
    {
        switch (code) {
            case HELLO_SVR_CMD_SAYHELLO: {
                sayhello();
                return NO_ERROR;
            } break;
            case HELLO_SVR_CMD_SAYHELLO_TO: {
                //从 data取出参数入参
                uint32_t params = data.readInt32();
                const String16 name16 = data.readString16();
                //String16 -> char *
                String8 name8(name16);
                int i = sayhello_to(name8.string());
                //并且把返回值写入reply回复回去
                reply->writeInt32(i);
                return NO_ERROR;
            } break;
            default:
                return BBinder::onTransact(code, data, reply, flags);
        }
    }

    void BnGodvService::sayhello(void)
    {
        static int cnt = 0;
        ALOGI("sayhello : %d\n", cnt++);
    }

    int BnGodvService::sayhello_to(const char *name)
    {
        static int cnt = 9002;
        ALOGI("sayhello_to %s : %d\n", name, cnt++);
        return cnt;
    }

};


packages/apps/Godv/BpGodvService.cpp

//
// Created by godv on 21-6-29.
// Bp Binder proxy 客户端要使用的 发送类
//参考  frameworks/av/media/libmedia/IMediaPlayerService.cpp

#include "IGodvService.h"

namespace android{

//相当于 BpGodvService : IGodvService, BpRefBase
class BpGodvService: public BpInterface<IGodvService>
{
public:
    explicit BpGodvService(const sp<IBinder>& impl)
        : BpInterface<IGodvService>(impl)
    {

    }

    void sayhello(void)
    {
        //构造 发送数据
        Parcel data, reply;
        data.writeInt32(0);
        remote()->transact(HELLO_SVR_CMD_SAYHELLO, data, &reply);
    }

    int sayhello_to(const char *name)
    {
        //构造 发送数据
        Parcel data, reply;
        data.writeInt32(0);
        //char * -> String16
        data.writeString16(String16(name));
        remote()->transact(HELLO_SVR_CMD_SAYHELLO_TO, data, &reply);
        return reply.readInt32();
    }
};

//实现必须的接口
IMPLEMENT_META_INTERFACE(GodvService, "android.media.IGodvService");

};

packages/apps/Godv/GodvService.cpp

//
// Created by godv on 21-6-29.
//参考  frameworks/av/media/mediaserver/main_mediaserver.cpp
#define LOG_TAG "GodvService"

#include <binder/IPCThreadState.h>
#include <binder/ProcessState.h>
#include <binder/IServiceManager.h>
#include <utils/Log.h>
#include "IGodvService.h"

using namespace android;

int main(void)
{
    //add service
    //循环 {读数据  解析数据  调用服务函数}

    //打开驱动 mmap
    sp<ProcessState> proc(ProcessState::self());

    //获得BpServiceManager
    sp<IServiceManager> sm(defaultServiceManager());

    sm->addService(String16("godv"),new BnGodvService());
    //循环
    ProcessState::self()->startThreadPool();
    IPCThreadState::self()->joinThreadPool();

    return 0;
}


packages/apps/Godv/GodvClient.cpp

//
// Created by godv on 21-6-29.
//
#define LOG_TAG "GodvService"

#include <binder/IBinder.h>
#include <binder/IPCThreadState.h>
#include <binder/ProcessState.h>
#include <binder/IServiceManager.h>
#include <utils/Log.h>
#include "IGodvService.h"

using namespace android;

int main(int argc,char **argv)
{
    ALOGI("Usage:\n");
    if(argc < 2){
        ALOGI("Usage:\n");
        ALOGI("%s hello\n",argv[0]);
        ALOGI("%s hello <name>\n",argv[0]);
    }

    //getService
    //打开驱动 mmap
    sp<ProcessState> proc(ProcessState::self());

    //获得BpServiceManager
    sp<IServiceManager> sm(defaultServiceManager());
    sp<IBinder> binder =
        sm->getService(String16("godv"));

    if(binder == 0)
    {
        ALOGI("can not get godv service\n");
        return -1;
    }

    sp<IGodvService> service =
        interface_cast<IGodvService>(binder);

    int cnt;
    if(argc == 2){
        service->sayhello();
        ALOGI("client call sayhello\n");
    } else if (argc == 3){
        cnt = service->sayhello_to(argv[2]);
        ALOGI("client call sayhello_to cnt = %d\n", cnt);
    }
    return 0;
}

packages/apps/Godv/Android.mk

LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)

LOCAL_SRC_FILES:= \
        GodvService.cpp \
        BnGodvService.cpp \
        BpGodvService.cpp

LOCAL_SHARED_LIBRARIES := \
        liblog \
        libutils \
        libbinder

LOCAL_MODULE:= godvserverc

include $(BUILD_EXECUTABLE)



include $(CLEAR_VARS)

LOCAL_SRC_FILES:= \
        GodvClient.cpp \
        BpGodvService.cpp

LOCAL_SHARED_LIBRARIES := \
        liblog \
        libutils \
        libbinder

LOCAL_MODULE:= godvclientc

include $(BUILD_EXECUTABLE)

generic_x86_64:/data/local # logcat GodvService:* *:S & 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值