android binder c++層-客戶端(c++) 呼叫 服務端(c++) 例子

BarbieGan發表於2014-11-20
客戶端和服務端都採用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 FUNC_CALLFUNCTION 1
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;
      int ret = ibinder->transact(FUNC_CALLFUNCTION, _data, &_reply, 0);
     LOGD( "Client transact return %d", ret);
     ProcessState::self()->startThreadPool();
     IPCThreadState::self()->joinThreadPool();
      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 FUNC_CALLFUNCTION 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 FUNC_CALLFUNCTION:
              callFunction();
               break;
           default:
               return BBinder::onTransact(code, data, reply, flags);
          }
           return 0;
     }
private :
     String16 mydescriptor;
};

int main() {
     sp < IServiceManager > sm = defaultServiceManager();
     SampleService* samServ = new SampleService();
     status_t ret = sm->addService(String16(SAMPLE_SERIVCE_DES), samServ);
     LOGD( "Service addservice return %d", ret);
     ProcessState::self()->startThreadPool();
     IPCThreadState::self()->joinThreadPool( true);
     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,然後各自執行


相關文章