Native Binder 实例
2014-10-08 11:04
363 查看
<span style="font-family: Arial, Helvetica, sans-serif;">该实例是NATIVE实例,在ANDROID系统编译后可直接通信</span>
主要包括四个文件:TestServer.cpp, TestClient.cpp ITestService.cpp Test.h
具体代码如下:
TestServer.cpp
#include "Test.h" namespace android { class BnTestService: public BnInterface<ITestService> { public: virtual status_t onTransact(uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags = 0); virtual void test() { printf("Now get test\n"); } }; status_t BnTestService::onTransact(uint_t code, const Parcel& data, Parcel* reply, uint32_t flags) { switch (code) { case TEST: { printf("got the client msg\n"); CHECK_INTERFACE(ITest, data, reply); test(); reply->writeInt32(100); return NO_ERROR; } break; case ZZTEST: printf("ZZTEST got the client msg\n"); CHECK_INTERFACE(ITest, data, reply); reply->writeInt32(100); break; default: break; } return NO_ERROR; } } int main() { sp < ProcessState > proc(ProcessState::self()); sp < IServiceManager > sm = defaultServiceManager(); sm->addService(String16("service.testservice"), new BnTestService()); ProcessState::self()->startThreadPool(); IPCThreadState::self()->joinThreadPool(); return 0; }
TestClient.cpp
#include <Test.h> namespace android{ BpTestService::BpTestService(const sp < IBinder > & impl): BpInterface<ITestService>(impl){ } void BpTestService::test() { printf("in the get Test\n"); Parcel data, reply; data.writeInterfaceToken(ITestService::getInterfaceDescriptor()); remote()->transact(TEST,data, &reply); printf("send Print %d\n", reply.readInt32()); } } int main (void) { sp <IServiceManager> sm = defaultServiceManager(); sp <IBinder> binder = sm->getService(String16("service.testservice")); sp <ITestService> cs = interface_cast <ITestService> (binder); cs->test(); return 0; }
ITestService.cpp
#include "Test.h" namespace android { IMPLEMENT_META_INTERFACE(TestService, "android.TestServer.ITestService"); }
Test.h
#ifndef TEST_H_H #define TEST_H_H #include <stdio.h> #include <binder/IInterface.h> #include <binder/Parcel.h> #include <binder/IBinder.h> #include <binder/Binder.h> #include <binder/ProcessState.h> #include <binder/IPCThreadState.h> #include <binder/IServiceManager.h> using namespace android; namespace android { class ITestService : public IInterface { public: DECLARE_META_INTERFACE(TestService); // declare macro virtual void test()=0; }; enum { TEST = IBinder::FIRST_CALL_TRANSACTION, ZZTEST, }; class BpTestService: public BpInterface<ITestService> { public: BpTestService(const sp<IBinder>& impl); virtual void test(); }; } #endif
Android.mk
LOCAL_PATH := $(call my-dir)
#生成binder service的服务端
include $(CLEAR_VARS)
LOCAL_SHARED_LIBRARIES := \
libcutils \
libutils \
libbinder
LOCAL_MODULE := TestServer
LOCAL_SRC_FILES := \
TestServer.cpp \
ITestService.cpp
LOCAL_MODULE_TAGS := optional
include $(BUILD_EXECUTABLE)
#生成binder service的测试client端
include $(CLEAR_VARS)
LOCAL_SHARED_LIBRARIES := \
libcutils \
libutils \
libbinder
LOCAL_MODULE := TestClient
LOCAL_SRC_FILES := \
TestClient.cpp \
ITestService.cpp
LOCAL_MODULE_TAGS := optional
include $(BUILD_EXECUTABLE)
相关文章推荐
- ReactNative跟Android原生模块是如何进行数据交互实例
- Android之JNI实例一--简单的Java调用Native方法
- java native方法及JNI实例
- React-Native实现ListView组件之上拉刷新实例(iOS和Android通用)
- java native方法及JNI实例
- React Native 小实例 采用View布局如下视图
- 实例分析android中的Binder通信机制(1)
- java native方法及JNI实例
- Binder简单实例
- 【React Native开发】React Native控件之Image组件讲解与美团首页顶部效果实例(10)
- webview中的js调用native的实例
- Binder_Native:MediaPlayerService和它的Client(针对getService流程)
- Android Service Binder交互通信实例
- java native方法及JNI实例
- ASP.NET MVC 2扩展点之Model Binder实例分析
- android libbinder – Binder的Native实现
- IOS React Native FlexBox详解及实例
- java native方法及JNI实例
- Binder_Native:匿名service
- ReactNative学习实例(八) 第三方组件TabNavigator底部导航栏