Android Binder机制的Native应用

mkdir testbinder  //创建testbinder目录

Android.mk

include $(call all-subdir-makefiles)

一、接口类

mkdir interface  //创建interface目录,存放接口类

Itestbinder.h

#include <binder/IInterface.h>
namespace android{
  class Itestbinder : public IInterface{
    public:
      DECLARE_META_INTERFACE(testbinder);
      virtual int testinterface(int a) = 0;
  };
  /
  class Bntestbinder : public BnInterface<Itestbinder>{
  public:
    virtual status_t    onTransact( uint32_t code,
                                    const Parcel& data,
                                    Parcel* reply,
                                    uint32_t flags = 0);
  };
}
Itestbinder.cpp
#include "Itestbinder.h"
#include <binder/Parcel.h>
#include <binder/IInterface.h>
namespace android{
  enum {
    TEST_INTERFACE,
  };
//客户端
  class Bptestbinder : public BpInterface<Itestbinder>{
    public:
      Bptestbinder(const sp<IBinder>& impl) : BpInterface<Itestbinder>(impl){
      }
      virtual int testinterface(int a){
        LOGD("TK---->>>>>>Itestbinder.cpp>>>>Bptestbinder::testinterface\n");
        Parcel data,reply;
        data.writeInt32(a);
        remote()->transact(TEST_INTERFACE,data,&reply);
        return reply.readInt32();
      }
  };

  IMPLEMENT_META_INTERFACE(testbinder, "android.test.Itestbinder");
/服务端
  status_t Bntestbinder::onTransact(
      uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags){
    LOGD("TK---->>>>>>Itestbinder.cpp>>>>Bntestbinder::onTransact\n");
    switch (code) {
      case TEST_INTERFACE:{
        //CHECK_INTERFACE(Itestbinder, data, reply);
        LOGD("TK---->>>>>>Itestbinder.cpp>>>>Bntestbinder::onTransact>>111\n");
        reply->writeInt32(testinterface((int) data.readInt32()) );
        return NO_ERROR;
      } break;
      default:{
        LOGD("TK---->>>>>>Itestbinder.cpp>>>>Bntestbinder::onTransact>>222\n");
        return BBinder::onTransact(code, data, reply, flags);
      }
    }
  }
}

二、客户端实现

mkdir client  //创建client目录,这个是client的实现

client.h

#include "../interface/Itestbinder.h"

namespace android{
  class client{
    public:
      static const sp<Itestbinder>& get_test_binder();
      static sp<Itestbinder> gtestbinder;
  };
}
client.cpp
#include "client.h"
#include <binder/IServiceManager.h>
#include <utils/Log.h>
#include <stdio.h>
namespace android{
  sp<Itestbinder> client::gtestbinder;
  const sp<Itestbinder>& client::get_test_binder(){
    if (gtestbinder == 0) {
        sp<IServiceManager> sm = defaultServiceManager();
        sp<IBinder> binder;
        do {
            binder = sm->getService(String16("test.Itestbinder"));
            if (binder != 0)
                break;
            printf("testbinder not published, waiting...");
            usleep(500000); // 0.5 s
        } while (true);
        gtestbinder = interface_cast<Itestbinder>(binder);
    }
    if(gtestbinder==0) printf("no testbinder!?");
    return gtestbinder;
  }
}
main.cpp
#include <stdio.h>
#include "client.h"

using namespace android;

int main(int argc, char* argv[]){
  client* myclient = new client();
  if(myclient == NULL) return 0;
  const sp<Itestbinder>& tb = myclient->get_test_binder();
  if(tb == NULL) return 0;
  int a = tb->testinterface(3);
  LOGD("TK-------->>>result is %d\n",a);
  delete myclient;
  return 0;
}

Android.mk

LOCAL_PATH:= $(call my-dir)
#LOCAL_CFLAGS_ALL :=-I. -I$(LOCAL_PATH)/..

include $(CLEAR_VARS)

LOCAL_SRC_FILES:= \
	client.cpp \
	main.cpp \
	../interface/Itestbinder.cpp

LOCAL_SHARED_LIBRARIES := \
        libui libcutils libutils libbinder libsonivox libicuuc libexpat \
	libdl


LOCAL_MODULE:= client
LOCAL_MODULE_TAGS := optional

include $(BUILD_EXECUTABLE)

三、服务端

mkdir server  //创建server目录,这个是服务端实现

testbinder.h

#include "../interface/Itestbinder.h"
#include <binder/BinderService.h>
namespace android{
  class testbinder: 
      public BinderService<testbinder>,
      public Bntestbinder{
    friend class BinderService<testbinder>;
    public:
      static const char* getServiceName() { return "test.Itestbinder"; }
      virtual int testinterface(int a);
      virtual     status_t    onTransact(
                                uint32_t code,
                                const Parcel& data,
                                Parcel* reply,
                                uint32_t flags);

  };
}
testbinder.cpp
#include <binder/IPCThreadState.h>
#include <binder/IServiceManager.h>
#include <utils/Log.h>
//#include <utils/Trace.h>
#include <binder/Parcel.h>
#include <binder/IPCThreadState.h>
#include <utils/String16.h>
#include <utils/threads.h>
#include <utils/Atomic.h>

//#include <cutils/bitops.h>
#include <cutils/properties.h>
#include <cutils/compiler.h>
#include "testbinder.h"

namespace android{
  int testbinder::testinterface(int a){
    LOGD("TK---->>>>>>testbinder.cpp>>>>testbinder::testinterface\n");
    return a+2;
  }
  status_t testbinder::onTransact(
        uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags){
    LOGD("TK---->>>>>>testbinder.cpp>>>>testbinder::onTransact\n");
    return Bntestbinder::onTransact(code, data, reply, flags);
  }
}
main.cpp
#include <binder/IPCThreadState.h>
#include <binder/ProcessState.h>
#include <binder/IServiceManager.h>
#include <utils/Log.h>

#include "testbinder.h"


using namespace android;

int main(int argc, char** argv)
{
  sp<ProcessState> proc(ProcessState::self());
  sp<IServiceManager> sm = defaultServiceManager();
  LOGI("ServiceManager: %p", sm.get());
  testbinder::instantiate();
  ProcessState::self()->startThreadPool();
  IPCThreadState::self()->joinThreadPool();
  return 0;
}

Android.mk

LOCAL_PATH:= $(call my-dir)
#LOCAL_CFLAGS_ALL :=-I. -I$(LOCAL_PATH)/..

include $(CLEAR_VARS)

LOCAL_SRC_FILES:= \
	main.cpp \
	testbinder.cpp \
	../interface/Itestbinder.cpp

LOCAL_SHARED_LIBRARIES := \
        libui libcutils libutils libbinder libsonivox libicuuc libexpat \
	libdl


LOCAL_MODULE:= server
LOCAL_MODULE_TAGS := optional

include $(BUILD_EXECUTABLE)

四、运行

./server

./client

结果:

result:

1.client
root@android:/data # ./client                                                  
TK---->>>>>>Itestbinder.cpp>>>>Bptestbinder::testinterface
TK-------->>>result is 4

2.server
root@android:/data # ./server                                                  
TK---->>>>>>testbinder.cpp>>>>testbinder::onTransact
TK---->>>>>>Itestbinder.cpp>>>>Bntestbinder::onTransact
TK---->>>>>>Itestbinder.cpp>>>>Bntestbinder::onTransact>>111
TK---->>>>>>testbinder.cpp>>>>testbinder::testinterface

=============================================================
I/        (  624): ServiceManager: 0xc708
D/        (  626): TK---->>>>>>Itestbinder.cpp>>>>Bptestbinder::testinterface
D/        (  624): TK---->>>>>>testbinder.cpp>>>>testbinder::onTransact
D/        (  624): TK---->>>>>>Itestbinder.cpp>>>>Bntestbinder::onTransact
D/        (  624): TK---->>>>>>Itestbinder.cpp>>>>Bntestbinder::onTransact>>111
D/        (  624): TK---->>>>>>testbinder.cpp>>>>testbinder::testinterface
D/        (  626): TK-------->>>result is 5


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值