粗浅的 C++ Binder 服务和客户例子
只支持注册服务获取服务,很粗浅,留底备用
目录结构
.
├── Android.mk
├── include
│ └── IMyBinder.h
├── lib
│ ├── Android.mk
│ └── IMyBinder.cpp
├── server
│ ├── Android.mk
│ ├── main.cpp
│ ├── MyBinder.cpp
│ └── MyBinder.h
└── test
├── Android.mk
└── test.cpp
IMyBinder.h
#include <stdint.h> #include <sys/types.h> #include <utils/RefBase.h> #include <utils/Errors.h> #include <binder/IInterface.h> namespace android { // ---------------------------------------------------------------------------- class IMyBinder : public IInterface { public: DECLARE_META_INTERFACE(MyBinder); }; // ---------------------------------------------------------------------------- class BnMyBinder : public BnInterface<IMyBinder> { public: virtual status_t onTransact( uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags = 0); }; // ---------------------------------------------------------------------------- }; // namespace android
IMyBinder.cpp
#define LOG_TAG "MyBinder" #include <stdint.h> #include <sys/types.h> #include <binder/Parcel.h> #include "IMyBinder.h" namespace android { class BpMyBinder : public BpInterface<IMyBinder> { public: BpMyBinder(const sp<IBinder>& impl) : BpInterface<IMyBinder>(impl) { } }; IMPLEMENT_META_INTERFACE(MyBinder, "demo.IMyBinder"); status_t BnMyBinder::onTransact( uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) { switch(code) { default: return BBinder::onTransact(code, data, reply, flags); } } }; // namespace android
MyBinder.h
#include <binder/BinderService.h> #include "IMyBinder.h" namespace android { class MyBinder : public BinderService<MyBinder>, public BnMyBinder, protected Thread { public: static char const* getServiceName() { return "demo.MyBinder"; } MyBinder(); virtual ~MyBinder(); virtual status_t onTransact( uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags); virtual bool threadLoop(); }; }; // namespace android
MyBinder.cpp
#include "MyBinder.h" namespace android { MyBinder::MyBinder() : BnMyBinder(), Thread(false) { } MyBinder::~MyBinder() { } status_t MyBinder::onTransact( uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) { switch (code) { default: return BBinder::onTransact(code, data, reply, flags); } return NO_ERROR; } bool MyBinder::threadLoop() { return true; } }; // namespace android
main.cpp
#include <binder/BinderService.h> #include "MyBinder.h" using namespace android; int main(int argc, char** argv) { MyBinder::publishAndJoinThreadPool(); return 0; }
test.cpp
#include <utils/RefBase.h> #include <utils/Log.h> #include <binder/IServiceManager.h> #include "IMyBinder.h" using namespace android; sp<IMyBinder> gMyBinder; const sp<IMyBinder>& getMyBinderService() { sp<IServiceManager> sm = defaultServiceManager(); sp<IBinder> binder; do { binder = sm->getService(String16("demo.MyBinder")); if (binder != 0) break; LOGW("Service not published, waiting..."); usleep(500000); // 0.5 s } while(true); /*if (mDeathNotifier == NULL) { mDeathNotifier = new DeathNotifier(); } binder->linkToDeath(mDeathNotifier);*/ gMyBinder = interface_cast<IMyBinder>(binder); LOGE_IF(gMyBinder==0, "no Service!?"); return gMyBinder; } int main() { getMyBinderService(); printf("get instance %p\n", gMyBinder.get()); return 0; }
Android.mk 贴 server 下的一个,其他类似修改
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= \
MyBinder.cpp \
main.cpp
LOCAL_SHARED_LIBRARIES := \
libmybinder libutils libbinder
LOCAL_C_INCLUDES:= $(LOCAL_PATH)/../include \
frameworks/base/include/
LOCAL_MODULE_TAGS := debug
LOCAL_MODULE:= mybinder
include $(BUILD_EXECUTABLE)