#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;
}