手把手教你如何创建一个连接到Binder上的服务(图文)


1 概述

       大家都知道在Android下的IPC机制是Binder,它可以实现两个进程之间的通信。有关Binder的介绍网上太多,这里就不费话,OK,还是进入这篇文章的主题,即教你如何创建一个连接到Binder上的服务.并且这个示例中的源代码是保证可以原样编译通过的.

      在开始之前,我们首先来简单介绍一下我们即将制作的服务ExampleServer, 这个示例服务由主程序加上一个libExample.so文件组成,libExample.so用来实现对Client端实现的接口,而主程序就是用来启动这个服务的.费话不说了,下面进入正题.

2 步骤

第1步:生成ExampleService.so文件

1: 在framework/base目录下新建一个目录,用来保存libExample.so的源码

  1. $cd framework/base/  
  2. $mkdir ExampleService  
手把手教你如何创建一个连接到Binder上的服务(图文)

进入此目录:

  1. $cd ExampleService  
新建3个文件:ExampleService.h ,ExampleService.cpp,Android.mk

其中ExampleService.h文件的内容如下:

  1. // File: ExampleService.h   
  2. #ifndef ANDROID_EXAMPLE_SERVICE_H   
  3. #define ANDROID_EXAMPLE_SERVICE_H   
  4. #include <utils/threads.h>   
  5. #include <utils/RefBase.h>   
  6. #include <binder/IInterface.h>   
  7. #include <binder/BpBinder.h>   
  8. #include <binder/Parcel.h>   
  9.   
  10. namespace android {  
  11.     class ExampleService : public BBinder  
  12.     {  
  13.         mutable Mutex mLock;  
  14.         int32_t mNextConnId;  
  15.         public:  
  16.             static int instantiate();  
  17.             ExampleService();  
  18.             virtual ~ExampleService();  
  19.             virtual status_t onTransact(uint32_t, const Parcel&, Parcel*, uint32_t);  
  20.     };  
  21. }; //namespace   
  22. #endif  

ExampleService.cpp文件的内容如下:

  1. // File: ExampleService.cpp   
  2. #include "ExampleService.h"   
  3. #include <binder/IServiceManager.h>   
  4. #include <binder/IPCThreadState.h>   
  5.   
  6. namespace android {  
  7.   
  8.     static struct sigaction oldact;  
  9.     static pthread_key_t sigbuskey;  
  10.       
  11.     int ExampleService::instantiate()  
  12.     {  
  13.         LOGE("ExampleService instantiate");  
  14.         // 调用ServiceManager的addService方法进行系统服务注册,这样客户端程序就可以通过ServiceManager获得此服务的代理对象,从而请求其提供的服务   
  15.         int r = defaultServiceManager()->addService(String16("byn.example"), new ExampleService());  
  16.         LOGE("ExampleService r = %d/n", r);  
  17.         return r;  
  18.     }  
  19.   
  20.     ExampleService::ExampleService()  
  21.     {   
  22.         LOGV("ExampleService created");  
  23.         mNextConnId = 1;  
  24.         pthread_key_create(&sigbuskey, NULL);  
  25.     }  
  26.   
  27.     ExampleService::~ExampleService()  
  28.     {  
  29.         pthread_key_delete(sigbuskey);  
  30.         LOGV("ExampleService destroyed");  
  31.     }  
  32.     // 每个系统服务都继承自BBinder类,都应重写BBinder的onTransact虚函数。当用户发送请求到达Service时,系统框架会调用Service的onTransact函数   
  33.     status_t ExampleService::onTransact(uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)  
  34.     {  
  35.         switch(code)  
  36.         {  
  37.             case 0: {  
  38.                 pid_t pid = data.readInt32();  
  39.                 int num   = data.readInt32();  
  40.                 num = num + 100;  
  41.                 reply->writeInt32(num);  
  42.                 return NO_ERROR;  
  43.                 }  
  44.                 break;  
  45.             default:  
  46.                 return BBinder::onTransact(code, data, reply, flags);  
  47.         }  
  48.     }  
  49. }; //namespace  
Android.mk文件的内容如下:
  1. # File: Android.mk  
  2. LOCAL_PATH:= $(call my-dir)  
  3. include $(CLEAR_VARS)  
  4. LOCAL_SRC_FILES:= \  
  5.     ExampleService.cpp  
  6. LOCAL_C_INCLUDES := $(JNI_H_INCLUDE)  
  7. LOCAL_SHARED_LIBRARIES :=\  
  8.     libutils libbinder  
  9. LOCAL_MODULE_TAGS :optional  
  10. LOCAL_PRELINK_MODULE :false  
  11. LOCAL_MODULE :libExample  
  12.   
  13. include $(BUILD_SHARED_LIBRARY)  
  • 1
  • 2
  • 3
  • 下一页

相关内容