千里马Android Framework实战开发-native程序之间binder通信实战案例分析
Posted Android高级知识分享官
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了千里马Android Framework实战开发-native程序之间binder通信实战案例分析相关的知识,希望对你有一定的参考价值。
csdn在线学习课程,课程咨询答疑和新课信息:QQ交流群:422901085进行课程讨论
千里马android Framework实战开发-native程序之间binder通信实战案例分析
1、需求背景
在android系统开发中,不仅仅我们都停留在java层面,很可能也会遇到比如一些native的可执行程序,比如surfaceflinger,bootanimation等他们都是native的执行程序,这些程序之间也经常会有要进行binder跨进程通信的需求,所以这个时候学习native层进行跨进程通信是非常有必要的。
2、案例功能介绍
在app的java层面我们之前已经实现过了binder的双向通信,这里在c++层也一样来实现一个双向的binder通信
3、源码展示
client端:
#include <binder/IServiceManager.h>
#include <binder/IBinder.h>
#include <binder/Parcel.h>
#include <binder/ProcessState.h>
#include <binder/IPCThreadState.h>
#include <private/binder/binder_module.h>
#include <binder/IInterface.h>
#include <binder/Parcel.h>
#include <binder/Binder.h>
using namespace android;
#ifdef LOG_TAG
#undef LOG_TAG
#endif
#define LOG_TAG "binderCallbackClient"
#define SAMPLE_SERIVCE_DES "my_hello"
#define SAMPLE_CB_SERIVCE_DES "android.os.SampleCallback"
#define SRV_CODE 1
#define CB_CODE 1
class SampeCallback : public BBinder
public:
SampeCallback()
ALOGE("Client ------------------------------ %d",__LINE__);
mydescriptor = String16(SAMPLE_CB_SERIVCE_DES);
virtual ~SampeCallback()
virtual const String16& getInterfaceDescriptor() const
return mydescriptor;
protected:
void callbackFunction(int val)
ALOGE(" -----------callback Client ok------------------- %d val = %d",__LINE__,val);
virtual status_t onTransact( uint32_t code, const Parcel& data,Parcel* reply,uint32_t flags = 0)
ALOGD( "my_hello Client onTransact, line = %d, code = %d",__LINE__,code);
int val_1,val_2,val_3;
String8 str_1,str_2,str_3;
switch (code)
case CB_CODE:
//1.读取int32类型数据
val_1 = data.readInt32();
val_2 = data.readInt32();
val_3 = data.readInt32();
//2.读取String8类型字符串;str_1.string()-->String8转换char类型数组
str_1 = data.readString8();
str_2 = data.readString8();
str_3 = data.readString8();
callbackFunction(1234567);
break;
default:
return BBinder::onTransact(code, data, reply, flags);
return 0;
private:
String16 mydescriptor;
;
int main()
sp<IServiceManager> sm = defaultServiceManager();
sp<IBinder> ibinder = sm->getService(String16(SAMPLE_SERIVCE_DES));
if (ibinder == NULL)
return -1;
Parcel _data,_reply;
SampeCallback *callback = new SampeCallback();
//写入客户端的callback
_data.writeStrongBinder(sp<IBinder>(callback));
_data.writeInterfaceToken(String16(SAMPLE_CB_SERIVCE_DES));
int ret = ibinder->transact(SRV_CODE, _data, &_reply, 0);
printf("Client before joinThreadPool \\n");
IPCThreadState::self()->joinThreadPool();
printf("Client ------------------------------ main end");
return 0;
server端:
#include <binder/IServiceManager.h>
#include <binder/IBinder.h>
#include <binder/Parcel.h>
#include <binder/ProcessState.h>
#include <binder/IPCThreadState.h>
using namespace android;
#ifdef LOG_TAG
#undef LOG_TAG
#endif
#define LOG_TAG "sampleService"
#define SAMPLE_SERIVCE_DES "my_hello"
#define SAMPLE_CB_SERIVCE_DES "android.os.SampleCallback"
#define SRV_CODE 1
#define CB_CODE 1
class SampleService: public BBinder
public:
SampleService()
ALOGE("my_hello Server ------------------------------ %d",__LINE__);
mydescriptor = String16(SAMPLE_SERIVCE_DES);
virtual ~SampleService()
virtual const String16& getInterfaceDescriptor() const
return mydescriptor;
protected:
void callFunction(int val)
ALOGE("Server ------------------------------ %d",__LINE__);
ALOGI( "Service: %s(), %d, val = %d",__FUNCTION__,__LINE__,val);
virtual status_t onTransact(uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags = 0)
ALOGD( "Service onTransact,line = %d, code = %d",__LINE__, code);
switch (code)
case SRV_CODE:
//读取Client传过来的IBinder对象
callback = data.readStrongBinder();
if(callback != NULL)
Parcel _data, _reply;
_data.writeInt32(1);
_data.writeInt32(2);
_data.writeInt32(3);
//2.String8类型
_data.writeString8(String8("who..."));
_data.writeString8(String8("are..."));
_data.writeString8(String8("you..."));
//回调客户端
int ret = callback->transact(CB_CODE, _data, &_reply, 0);
//调用server端的
callFunction(6666);
break;
default:
return BBinder::onTransact(code, data, reply, flags);
return 0;
private:
String16 mydescriptor;
sp<IBinder> callback;
;
int main()
sp<IServiceManager> sm = defaultServiceManager();
SampleService* samServ = new SampleService();
status_t ret = sm->addService(String16(SAMPLE_SERIVCE_DES), samServ);
printf("server before joinThreadPool \\n");
IPCThreadState::self()->joinThreadPool( true);
printf("server after joinThreadPool \\n");
return 0;
以上是关于千里马Android Framework实战开发-native程序之间binder通信实战案例分析的主要内容,如果未能解决你的问题,请参考以下文章
千里马Android Framework实战开发-native程序之间binder通信实战案例分析
千里马Android Framework实战开发-native程序之间binder通信实战案例分析
千里马Android Framework实战开发-跨进程通信专题博客总结
千里马Android Framework实战开发-跨进程通信专题课表介绍
千里马android framework实战开发-binder驱动之oneway导致的transaction failed
千里马android framework实战开发-binder驱动之oneway导致的transaction failed