android c++服务,android中c++层binder简单例子

#include

#include

#include

#include

#include

using namespace android;

#ifdef LOG_TAG

#undef LOG_TAG

#endif

#define LOG_TAG "testService"

class MyService : public BBinder

{

public:

MyService()

{

mydescriptor = String16("media.hello");

n=0;

}

virtual ~MyService() {}

//This function is used when call Parcel::checkInterface(IBinder*)

virtual const String16& getInterfaceDescriptor() const

{

LOGE("this is enter ==========getInterfaceDescriptor");

return mydescriptor;

}

protected:

void show()

{

LOGE("this is for test show!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");

LOGE("this is for test show!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");

LOGE("this is for test show!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");

LOGE("this is for test show!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");

LOGE("this is for test show!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");

}

virtual status_t onTransact( uint32_t code,

const Parcel& data,

Parcel* reply,

uint32_t flags = 0)

{

LOGD("enter MyService onTransact and the code is %d",code);

/*

if (data.checkInterface(this))

LOGD("checkInterface OK");

else

{

LOGW("checkInterface failed");

return BBinder::onTransact(code, data, reply, flags);

}

*/

switch (code)

{

case 1:

LOGD("MyService interface 1");

break;

case 2:

LOGD("MyService interface 2");

cb = data.readStrongBinder();

break;

case 3:

{

LOGD("MyService interface 3, exit");

//No unregister service routine?

//It should return to client first and then call exit in another place.

exit(0);

break;

}

case 4:

{//call cb

LOGD("MyService interface 4 before if================");

cb = data.readStrongBinder();

if (cb != NULL)

{

LOGD("MyService interface 4");

Parcel in, out;

in.writeInterfaceToken(String16("ghq.callback"));

in.writeInt32(n++);

in.writeString16(String16("This is a string."));

cb->transact(1, in, &out, 0);

show();

}

break;

}

default:

return BBinder::onTransact(code, data, reply, flags);

}

return 0;

}

private:

String16 mydescriptor;

spcb;

int n;

};

int main()

{

spsm = defaultServiceManager();

status_t ret;

//register MyService to ServiceManager

MyService* srv = new MyService();

ret = sm->addService(String16("media.hello"), srv);

LOGD("addservice media.hello return %d", ret);

//call binder thread pool to start

ProcessState::self()->startThreadPool();

IPCThreadState::self()->joinThreadPool(true);

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值