#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/file.h>//flock's head
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <binder/IServiceManager.h>
#include <binder/IBinder.h>
#include <binder/Parcel.h>
#include <binder/ProcessState.h>
#include <binder/IPCThreadState.h>
#include <android/log.h>
#include <cutils/properties.h>
#include "IService.h"
using namespace android;
#ifdef LOG_TAG
#undef LOG_TAG
#endif
#define TAG "testService"
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__)
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__)
#define LOGW(...) __android_log_print( ANDROID_LOG_WARN, TAG, __VA_ARGS__)
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
{
LOGD("checkInterface failed");
return BBinder::onTransact(code, data, reply, flags);
}
switch (code)
{
case TRANSACT_1:
LOGD("MyService interface 1");
printf("MyService interface 1");
break;
case TRANSACT_2:
LOGD("MyService interface 2");
printf("MyService interface 2");
cb = data.readStrongBinder();
break;
case TRANSACT_3:
{
LOGD("MyService interface 3, exit");
printf("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 TRANSACT_4:
{//call cb
LOGD("MyService interface 4 before if================");
cb = data.readStrongBinder();
if (cb != NULL)
{
LOGD("MyService interface 4");
printf("MyService interface 4");
Parcel in, out;
in.writeInterfaceToken(String16("android.os.ISetupCallback"));
in.writeInt32(n++);
in.writeString16(String16("This is a string."));
cb->transact(TRANSACT_1, in, &out, 0);
show();
}
break;
}
default:
return BBinder::onTransact(code, data, reply, flags);
}
return 0;
}
private:
String16 mydescriptor;
sp<IBinder> cb;
int n;
};
int main()
{
//LOGD("MyService start");
printf("MyService start\n");
sp<IServiceManager> sm = 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);
printf("addservice media.hello return %d", ret);
//call binder thread pool to start
ProcessState::self()->startThreadPool();
IPCThreadState::self()->joinThreadPool(true);
return 0;
}
没有合适的资源?快使用搜索试试~ 我知道了~
native直接使用Parcel通信
共5个文件
cpp:2个
mk:1个
so:1个
需积分: 29 7 下载量 33 浏览量
2017-03-14
17:12:12
上传
评论
收藏 25KB ZIP 举报
温馨提示
这个是native服务直接使用Parcel进行通信的一个接口,提供给后面使用。
资源推荐
资源详情
资源评论
收起资源包目录
Parcel.zip (5个子文件)
testCallback.cpp 3KB
libpaxapijni.so 65KB
testService.cpp 4KB
Android.mk 963B
IService.h 166B
共 5 条
- 1
资源评论
IT先森
- 粉丝: 4187
- 资源: 70
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功