binder c++层 服务端回调客户端
sampleClient.cpp ------------------------------------------------------------------------------------------------ #include  #include  #include  #include  #include  #include  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 sm = defaultServiceManager();      sp 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(callback));       int  ret = ibinder->transact(SRV_CODE, _data, &_reply, 0);
//     ProcessState::self()->startThreadPool(); //     IPCThreadState::self()->joinThreadPool();      while(1);       return  0; }
sampleService.cpp ------------------------------------------------------------------------------------------------ #include  #include  #include  #include  #include  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 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


更多相关文章

  1. 高通平台Android(安卓)N SystemUI添加qcnvitems qcrilhook
  2. Android(安卓)9编译报错sdclang设置
  3. Android之反编译
  4. AndroidStudio用gradle编译中文乱码
  5. Android(安卓)Studio编译失败:Error: Invoke-customs are only su
  6. Android(安卓)JNI和NDK学习(3)--Android.mk分析
  7. Android内核编译和裁剪
  8. android 自定义progressbar 样式
  9. Android(安卓)编译App报错 找不到android.support.annotation.Ke

随机推荐

  1. 第一篇博客
  2. 如何选择正确的因变量(控制变量),让你的计
  3. 儿时环境对其人生长远影响的综述, 包括前
  4. 他们只有本硕学历, 却发过了AER, JPE, QJ
  5. 气候经济学能发Nature,Science的三大热点
  6. nature首次出现经济学编辑!想发nature的学
  7. 什么时候需要标准化回归模型中的变量?
  8. 对数vs线性vs二次vs指数形式,到底选择哪种
  9. 机器学习方法出现在AER, JPE, QJE等顶刊
  10. 万字长文述"家庭经济学"进展, 诺奖得主贝