android binder c++层 - 回调客户端服务 - 客户端(c++层) 调用 服务端(c++层) 例子,服务端回调客户端服务

binder c++层 服务端回调客户端

sampleClient.cpp
------------------------------------------------------------------------------------------------
#include  <binder/IServiceManager.h>
#include  <binder/IBinder.h>
#include  <binder/Parcel.h>
#include  <binder/ProcessState.h>
#include  <binder/IPCThreadState.h>
#include  <private/binder/binder_module.h>
using  namespace  android;
#ifdef  LOG_TAG
#undef  LOG_TAG
#endif
#define  LOG_TAG  "sampleCallback"
#define  SAMPLE_SERIVCE_DES  "sample.hello"
#define  SAMPLE_CB_SERIVCE_DES  "android.os.SampleCallback"
#define  SRV_CODE 1
#define  CB_CODE 1
class  SampeCallback :  public  BBinder
{
      public :
     SampeCallback()
     {
          mydescriptor = String16(SAMPLE_CB_SERIVCE_DES);
     }
      virtual  ~SampeCallback() {
          
     }
      virtual  const  String16& getInterfaceDescriptor()  const {
            return  mydescriptor;
     }
      protected :
     
      void  callbackFunction() {
          LOGI(  "Client callback function called-----------cb"  );
     }
     
      virtual  status_t onTransact( uint32_t code,
                const  Parcel& data,Parcel* reply,uint32_t flags = 0){
          LOGD(  "Client onTransact, code = %d" , code);   
         
            switch  (code){
            case  CB_CODE:
              callbackFunction();
                break ;
            default :
                return  BBinder::onTransact(code, data, reply, flags);
          }
            return  0;
     }
      private :
     String16 mydescriptor;
};

int  main()
{
     sp<IServiceManager> sm = defaultServiceManager();
     sp<IBinder> ibinder = sm->getService(String16(SAMPLE_SERIVCE_DES));
      if  (ibinder == NULL){
          LOGW(  "Client can't find Service""  );
            return  -1;
     }
     Parcel _data,_reply;
     SampeCallback *callback =  new  SampeCallback();
     _data.writeStrongBinder(sp<IBinder>(callback));
      int  ret = ibinder->transact(SRV_CODE, _data, &_reply, 0);

//     ProcessState::self()->startThreadPool();
//     IPCThreadState::self()->joinThreadPool();
     while(1);
      return  0;
}

sampleService.cpp
------------------------------------------------------------------------------------------------
#include  <binder/IServiceManager.h>
#include  <binder/IBinder.h>
#include  <binder/Parcel.h>
#include  <binder/ProcessState.h>
#include  <binder/IPCThreadState.h>
using  namespace  android;
#ifdef  LOG_TAG
#undef  LOG_TAG
#endif
#define  LOG_TAG  "sampleService"
#define  SAMPLE_SERIVCE_DES  "sample.hello"
#define  SAMPLE_CB_SERIVCE_DES  "android.os.SampleCallback"
#define  SRV_CODE 1
#define  CB_CODE 1
class  SampleService:  public  BBinder {
public :
     SampleService() {
          mydescriptor = String16(SAMPLE_SERIVCE_DES);
     }
     
      virtual  ~SampleService() {
     }

      virtual  const  String16& getInterfaceDescriptor()  const  {
            return  mydescriptor;
     }
     
protected :
     
      void  callFunction() {
          LOGE(  "Service callFunction-----------" );
     }
     
      virtual  status_t onTransact(uint32_t code,  const  Parcel& data,
              Parcel* reply, uint32_t flags = 0) {
          LOGD(  "Service onTransact, code = %d"  , code);
            switch  (code) {
            case  SRV_CODE:
              callback = data.readStrongBinder();
                if  (callback != NULL)
              {
                   Parcel _data, _reply;
                   _data.writeInterfaceToken(String16(SAMPLE_CB_SERIVCE_DES));
                     int  ret = callback->transact(CB_CODE, _data, &_reply, 0);
              }
              callFunction();
                break ;
            default :
                return  BBinder::onTransact(code, data, reply, flags);
          }
            return  0;
     }
private :
     String16 mydescriptor;
     sp<IBinder> callback;
};

int  main() {
     sp < IServiceManager > sm = defaultServiceManager();
     SampleService* samServ =  new  SampleService();
     status_t ret = sm->addService(String16(SAMPLE_SERIVCE_DES), samServ);
     LOGD( "Service addservice" );
     ProcessState::self()->startThreadPool();
//     IPCThreadState::self()->joinThreadPool(  true );
     while(1);
      return  0;
}

Android.mk
------------------------------------------------------------------------------------------------
LOCAL_PATH: =  $(call my-dir)
include  $(CLEAR_VARS)

LOCAL_MODULE_TAGS : = eng

LOCAL_SRC_FILES: = \
       sampleClient.cpp

LOCAL_C_INCLUDES + = \
         $(LOCAL_PATH)  \

LOCAL_SHARED_LIBRARIES :  = \
    libcutils \
    libbinder \
    libutils \
    libhardware

LOCAL_CFLAGS : = -DRIL_SHLIB

LOCAL_MODULE: = sampleClient

include  $(BUILD_EXECUTABLE)

include  $(CLEAR_VARS)

LOCAL_MODULE_TAGS : = eng

LOCAL_SRC_FILES: = \
           sampleService.cpp

LOCAL_C_INCLUDES + = \
         $(LOCAL_PATH)  \

LOCAL_SHARED_LIBRARIES :  = \
    libcutils \
    libbinder \
    libutils \
    libhardware

LOCAL_CFLAGS : = -DRIL_SHLIB

LOCAL_MODULE: = sampleService

include  $(BUILD_EXECUTABLE)

------------------------------------------------
1、新建文件夹binderSample
2、将以上三个文件放入ics-4.x\external\binderSample
3、编译,拷贝到开发环境中,chmod,然后各自运行
 4、看log



  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值