歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 手把手教你如何創建一個連接到Binder上的服務(圖文)

手把手教你如何創建一個連接到Binder上的服務(圖文)

日期:2017/3/1 10:41:14   编辑:Linux編程

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

進入此目錄:

  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. namespace android {
  10. class ExampleService : public BBinder
  11. {
  12. mutable Mutex mLock;
  13. int32_t mNextConnId;
  14. public:
  15. static int instantiate();
  16. ExampleService();
  17. virtual ~ExampleService();
  18. virtual status_t onTransact(uint32_t, const Parcel&, Parcel*, uint32_t);
  19. };
  20. }; //namespace
  21. #endif

ExampleService.cpp文件的內容如下:

  1. // File: ExampleService.cpp
  2. #include "ExampleService.h"
  3. #include <binder/IServiceManager.h>
  4. #include <binder/IPCThreadState.h>
  5. namespace android {
  6. static struct sigaction oldact;
  7. static pthread_key_t sigbuskey;
  8. int ExampleService::instantiate()
  9. {
  10. LOGE("ExampleService instantiate");
  11. // 調用ServiceManager的addService方法進行系統服務注冊,這樣客戶端程序就可以通過ServiceManager獲得此服務的代理對象,從而請求其提供的服務
  12. int r = defaultServiceManager()->addService(String16("byn.example"), new ExampleService());
  13. LOGE("ExampleService r = %d/n", r);
  14. return r;
  15. }
  16. ExampleService::ExampleService()
  17. {
  18. LOGV("ExampleService created");
  19. mNextConnId = 1;
  20. pthread_key_create(&sigbuskey, NULL);
  21. }
  22. ExampleService::~ExampleService()
  23. {
  24. pthread_key_delete(sigbuskey);
  25. LOGV("ExampleService destroyed");
  26. }
  27. // 每個系統服務都繼承自BBinder類,都應重寫BBinder的onTransact虛函數。當用戶發送請求到達Service時,系統框架會調用Service的onTransact函數
  28. status_t ExampleService::onTransact(uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
  29. {
  30. switch(code)
  31. {
  32. case 0: {
  33. pid_t pid = data.readInt32();
  34. int num = data.readInt32();
  35. num = num + 100;
  36. reply->writeInt32(num);
  37. return NO_ERROR;
  38. }
  39. break;
  40. default:
  41. return BBinder::onTransact(code, data, reply, flags);
  42. }
  43. }
  44. }; //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. include $(BUILD_SHARED_LIBRARY)
Copyright © Linux教程網 All Rights Reserved