只支持注册服务获取服务,很粗浅,留底备用
目录结构
.
├── 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)