Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1071211
  • 博文数量: 403
  • 博客积分: 10272
  • 博客等级: 上将
  • 技术积分: 4407
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-24 14:22
文章分类

全部博文(403)

文章存档

2012年(403)

分类: 嵌入式

2012-04-26 21:07:56

只支持注册服务获取服务,很粗浅,留底备用

目录结构

.
├── 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 #include #include #include #include namespace android { // ---------------------------------------------------------------------------- class IMyBinder : public IInterface { public: DECLARE_META_INTERFACE(MyBinder); }; // ---------------------------------------------------------------------------- class BnMyBinder : public BnInterface { 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 #include #include #include "IMyBinder.h" namespace android { class BpMyBinder : public BpInterface { public: BpMyBinder(const sp& impl) : BpInterface(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 #include "IMyBinder.h" namespace android { class MyBinder : public BinderService, 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 #include "MyBinder.h" using namespace android; int main(int argc, char** argv) { MyBinder::publishAndJoinThreadPool(); return 0; }
复制代码

test.cpp

复制代码
#include #include #include #include "IMyBinder.h" using namespace android; sp gMyBinder; const sp& getMyBinderService() { sp sm = defaultServiceManager(); sp 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(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)

阅读(2222) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~