zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

Android Binder机制的Native应用

Android应用 机制 native Binder
2023-09-14 09:16:12 时间
mkdir testbinder  //创建testbinder目录

Android.mk

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. include $(call all-subdir-makefiles)  

一、接口类

mkdir interface  //创建interface目录,存放接口类

Itestbinder.h

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include <binder/IInterface.h>  
  2. namespace android{  
  3.   class Itestbinder : public IInterface{  
  4.     public:  
  5.       DECLARE_META_INTERFACE(testbinder);  
  6.       virtual int testinterface(int a) = 0;  
  7.   };  
  8.   /  
  9.   class Bntestbinder : public BnInterface<Itestbinder>{  
  10.   public:  
  11.     virtual status_t    onTransact( uint32_t code,  
  12.                                     const Parcel& data,  
  13.                                     Parcel* reply,  
  14.                                     uint32_t flags = 0);  
  15.   };  
  16. }  
Itestbinder.cpp
[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include "Itestbinder.h"  
  2. #include <binder/Parcel.h>  
  3. #include <binder/IInterface.h>  
  4. namespace android{  
  5.   enum {  
  6.     TEST_INTERFACE,  
  7.   };  
  8. //客户端  
  9.   class Bptestbinder : public BpInterface<Itestbinder>{  
  10.     public:  
  11.       Bptestbinder(const sp<IBinder>& impl) : BpInterface<Itestbinder>(impl){  
  12.       }  
  13.       virtual int testinterface(int a){  
  14.         LOGD("TK---->>>>>>Itestbinder.cpp>>>>Bptestbinder::testinterface\n");  
  15.         Parcel data,reply;  
  16.         data.writeInt32(a);  
  17.         remote()->transact(TEST_INTERFACE,data,&reply);  
  18.         return reply.readInt32();  
  19.       }  
  20.   };  
  21.   
  22.   IMPLEMENT_META_INTERFACE(testbinder, "android.test.Itestbinder");  
  23. /服务端  
  24.   status_t Bntestbinder::onTransact(  
  25.       uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags){  
  26.     LOGD("TK---->>>>>>Itestbinder.cpp>>>>Bntestbinder::onTransact\n");  
  27.     switch (code) {  
  28.       case TEST_INTERFACE:{  
  29.         //CHECK_INTERFACE(Itestbinder, data, reply);  
  30.         LOGD("TK---->>>>>>Itestbinder.cpp>>>>Bntestbinder::onTransact>>111\n");  
  31.         reply->writeInt32(testinterface((int) data.readInt32()) );  
  32.         return NO_ERROR;  
  33.       } break;  
  34.       default:{  
  35.         LOGD("TK---->>>>>>Itestbinder.cpp>>>>Bntestbinder::onTransact>>222\n");  
  36.         return BBinder::onTransact(code, data, reply, flags);  
  37.       }  
  38.     }  
  39.   }  
  40. }  

二、客户端实现

mkdir client  //创建client目录,这个是client的实现

client.h

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include "../interface/Itestbinder.h"  
  2.   
  3. namespace android{  
  4.   class client{  
  5.     public:  
  6.       static const sp<Itestbinder>& get_test_binder();  
  7.       static sp<Itestbinder> gtestbinder;  
  8.   };  
  9. }  
client.cpp
[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include "client.h"  
  2. #include <binder/IServiceManager.h>  
  3. #include <utils/Log.h>  
  4. #include <stdio.h>  
  5. namespace android{  
  6.   sp<Itestbinder> client::gtestbinder;  
  7.   const sp<Itestbinder>& client::get_test_binder(){  
  8.     if (gtestbinder == 0) {  
  9.         sp<IServiceManager> sm = defaultServiceManager();  
  10.         sp<IBinder> binder;  
  11.         do {  
  12.             binder = sm->getService(String16("test.Itestbinder"));  
  13.             if (binder != 0)  
  14.                 break;  
  15.             printf("testbinder not published, waiting...");  
  16.             usleep(500000); // 0.5 s  
  17.         } while (true);  
  18.         gtestbinder = interface_cast<Itestbinder>(binder);  
  19.     }  
  20.     if(gtestbinder==0) printf("no testbinder!?");  
  21.     return gtestbinder;  
  22.   }  
  23. }  
main.cpp
[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include <stdio.h>  
  2. #include "client.h"  
  3.   
  4. using namespace android;  
  5.   
  6. int main(int argc, char* argv[]){  
  7.   client* myclient = new client();  
  8.   if(myclient == NULL) return 0;  
  9.   const sp<Itestbinder>& tb = myclient->get_test_binder();  
  10.   if(tb == NULL) return 0;  
  11.   int a = tb->testinterface(3);  
  12.   LOGD("TK-------->>>result is %d\n",a);  
  13.   delete myclient;  
  14.   return 0;  
  15. }  

Android.mk

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. LOCAL_PATH:= $(call my-dir)  
  2. #LOCAL_CFLAGS_ALL :=-I. -I$(LOCAL_PATH)/..  
  3.   
  4. include $(CLEAR_VARS)  
  5.   
  6. LOCAL_SRC_FILES:= \  
  7.     client.cpp \  
  8.     main.cpp \  
  9.     ../interface/Itestbinder.cpp  
  10.   
  11. LOCAL_SHARED_LIBRARIES := \  
  12.         libui libcutils libutils libbinder libsonivox libicuuc libexpat \  
  13.     libdl  
  14.   
  15.   
  16. LOCAL_MODULE:= client  
  17. LOCAL_MODULE_TAGS := optional  
  18.   
  19. include $(BUILD_EXECUTABLE)  

三、服务端

mkdir server  //创建server目录,这个是服务端实现

testbinder.h

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include "../interface/Itestbinder.h"  
  2. #include <binder/BinderService.h>  
  3. namespace android{  
  4.   class testbinder:   
  5.       public BinderService<testbinder>,  
  6.       public Bntestbinder{  
  7.     friend class BinderService<testbinder>;  
  8.     public:  
  9.       static const char* getServiceName() { return "test.Itestbinder"; }  
  10.       virtual int testinterface(int a);  
  11.       virtual     status_t    onTransact(  
  12.                                 uint32_t code,  
  13.                                 const Parcel& data,  
  14.                                 Parcel* reply,  
  15.                                 uint32_t flags);  
  16.   
  17.   };  
  18. }  
testbinder.cpp
[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include <binder/IPCThreadState.h>  
  2. #include <binder/IServiceManager.h>  
  3. #include <utils/Log.h>  
  4. //#include <utils/Trace.h>  
  5. #include <binder/Parcel.h>  
  6. #include <binder/IPCThreadState.h>  
  7. #include <utils/String16.h>  
  8. #include <utils/threads.h>  
  9. #include <utils/Atomic.h>  
  10.   
  11. //#include <cutils/bitops.h>  
  12. #include <cutils/properties.h>  
  13. #include <cutils/compiler.h>  
  14. #include "testbinder.h"  
  15.   
  16. namespace android{  
  17.   int testbinder::testinterface(int a){  
  18.     LOGD("TK---->>>>>>testbinder.cpp>>>>testbinder::testinterface\n");  
  19.     return a+2;  
  20.   }  
  21.   status_t testbinder::onTransact(  
  22.         uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags){  
  23.     LOGD("TK---->>>>>>testbinder.cpp>>>>testbinder::onTransact\n");  
  24.     return Bntestbinder::onTransact(code, data, reply, flags);  
  25.   }  
  26. }  
main.cpp
[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include <binder/IPCThreadState.h>  
  2. #include <binder/ProcessState.h>  
  3. #include <binder/IServiceManager.h>  
  4. #include <utils/Log.h>  
  5.   
  6. #include "testbinder.h"  
  7.   
  8.   
  9. using namespace android;  
  10.   
  11. int main(int argc, char** argv)  
  12. {  
  13.   sp<ProcessState> proc(ProcessState::self());  
  14.   sp<IServiceManager> sm = defaultServiceManager();  
  15.   LOGI("ServiceManager: %p", sm.get());  
  16.   testbinder::instantiate();  
  17.   ProcessState::self()->startThreadPool();  
  18.   IPCThreadState::self()->joinThreadPool();  
  19.   return 0;  
  20. }  

Android.mk

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. LOCAL_PATH:= $(call my-dir)  
  2. #LOCAL_CFLAGS_ALL :=-I. -I$(LOCAL_PATH)/..  
  3.   
  4. include $(CLEAR_VARS)  
  5.   
  6. LOCAL_SRC_FILES:= \  
  7.     main.cpp \  
  8.     testbinder.cpp \  
  9.     ../interface/Itestbinder.cpp  
  10.   
  11. LOCAL_SHARED_LIBRARIES := \  
  12.         libui libcutils libutils libbinder libsonivox libicuuc libexpat \  
  13.     libdl  
  14.   
  15.   
  16. LOCAL_MODULE:= server  
  17. LOCAL_MODULE_TAGS := optional  
  18.   
  19. include $(BUILD_EXECUTABLE)  

四、运行

./server

./client

结果:

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. result:  
  2.   
  3. 1.client  
  4. root@android:/data # ./client                                                    
  5. TK---->>>>>>Itestbinder.cpp>>>>Bptestbinder::testinterface  
  6. TK-------->>>result is 4  
  7.   
  8. 2.server  
  9. root@android:/data # ./server                                                    
  10. TK---->>>>>>testbinder.cpp>>>>testbinder::onTransact  
  11. TK---->>>>>>Itestbinder.cpp>>>>Bntestbinder::onTransact  
  12. TK---->>>>>>Itestbinder.cpp>>>>Bntestbinder::onTransact>>111  
  13. TK---->>>>>>testbinder.cpp>>>>testbinder::testinterface  
  14.   
  15. =============================================================  
  16. I/        (  624): ServiceManager: 0xc708  
  17. D/        (  626): TK---->>>>>>Itestbinder.cpp>>>>Bptestbinder::testinterface  
  18. D/        (  624): TK---->>>>>>testbinder.cpp>>>>testbinder::onTransact  
  19. D/        (  624): TK---->>>>>>Itestbinder.cpp>>>>Bntestbinder::onTransact  
  20. D/        (  624): TK---->>>>>>Itestbinder.cpp>>>>Bntestbinder::onTransact>>111  
  21. D/        (  624): TK---->>>>>>testbinder.cpp>>>>testbinder::testinterface  
  22. D/        (  626): TK-------->>>result is 5