Native Service的完整示例

来源:互联网 发布:手机淘宝会员名怎么填 编辑:程序博客网 时间:2024/05/19 00:39


1.http://blog.csdn.net/yongyu_it/article/details/54629675



2.http://blog.csdn.net/zhx6044/article/details/47342227



AOSP版本:Android-7-0-0_r6

项目结构:位于android-7-0-0_r6\frameworks\native\services\thinking_test下


1、service:native service的主体,即服务的实现部分

2、server:native service的载体,即启动和注册服务的部分。

3、client:客户端程序,服务调用封装。

4、test:测试client

------------------------------------------------------------------------------------------------------------------------------------------

service


ThinkingService.h
[cpp] view plain copy
  1. #ifndef THINKING_SERVICE_H  
  2. #define THINKING_SERVICE_H  
  3.   
  4. #include <utils/RefBase.h>  
  5. #include <binder/IInterface.h>  
  6. #include <binder/Parcel.h>  
  7.   
  8. #include <android/log.h>  
  9. #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR  , "ProjectName", __VA_ARGS__)  
  10.   
  11. namespace android{  
  12.     class ThinkingService : public BBinder  
  13.     {  
  14.         private:  
  15.           
  16.         public:  
  17.         //单例模式  
  18.         static int Instance();  
  19.         ThinkingService();  
  20.         virtual ~ThinkingService();  
  21.         //预定义通信方法,签名写死  
  22.         //参数意义是:通信状态,输入参数,输出参数,执行状态  
  23.         virtual status_t onTransact(uint32_t, const Parcel&, Parcel*, uint32_t);  
  24.     };  
  25. }  
  26.   
  27. #endif  
ThinkingService.cpp
[cpp] view plain copy
  1. #include <binder/IServiceManager.h>  
  2. #include <binder/IPCThreadState.h>  
  3. #include "ThinkingService.h"  
  4.   
  5. namespace android  
  6. {  
  7.     static pthread_key_t sigbuskey;  
  8.     int ThinkingService::Instance()  
  9.     {  
  10.         LOGE("-----------ThinkingService-->Instance \n");  
  11.         int ret=defaultServiceManager()->addService( String16("thinking.svc"),new ThinkingService());  
  12.         LOGE("-----------ThinkingService-->Instance %d \n",ret);  
  13.         return ret;  
  14.     }  
  15.       
  16.     ThinkingService::ThinkingService()  
  17.     {  
  18.         LOGE("-----------ThinkingService");  
  19.         pthread_key_create(&sigbuskey,NULL);  
  20.     }  
  21.       
  22.     ThinkingService::~ThinkingService()  
  23.     {  
  24.         LOGE("-----------~ThinkingService");  
  25.         pthread_key_delete(sigbuskey);  
  26.     }  
  27.       
  28.     status_t ThinkingService::onTransact(uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)  
  29.     {  
  30.         switch(code)  
  31.         {  
  32.             case 0:  
  33.             {  
  34.                 int a=data.readInt32();  
  35.                 int b=data.readInt32();  
  36.                 reply->writeInt32(a*b);  
  37.                 return NO_ERROR;  
  38.             }  
  39.             default:  
  40.                 return BBinder::onTransact(code, data, reply, flags);  
  41.         }  
  42.     }  
  43. }  
Android.mk
[python] view plain copy
  1. LOCAL_PATH:=$(call my-dir)  
  2. include $(CLEAR_VARS)  
  3. LOCAL_SRC_FILES:=ThinkingService.cpp  
  4. LOCAL_SHARED_LIBRARIES:=libutils libbinder  
  5. LOCAL_MODULE_TAGS:=optional  
  6. LOCAL_MODULE:=libThinkingService  
  7. LOCAL_PRELINK_MODULE:=false  
  8. LOCAL_LDLIBS +=  -llog  
  9. include $(BUILD_SHARED_LIBRARY)  
------------------------------------------------------------------------------------------------------------------------------------------

server



ThinkingServer.cpp
[cpp] view plain copy
  1. #include <stdio.h>  
  2. #include <sys/types.h>  
  3. #include <unistd.h>  
  4. #include <grp.h>  
  5. #include <binder/IPCThreadState.h>  
  6. #include <binder/ProcessState.h>  
  7. #include <binder/IServiceManager.h>  
  8. #include <utils/Log.h>  
  9. #include "../service/ThinkingService.h"  
  10.   
  11. using namespace android;  
  12.   
  13. int main(int arg, char** argv)  
  14. {  
  15.     printf("ThinkingService start register \n");  
  16.     sp<ProcessState> proc(ProcessState::self());  
  17.     sp<IServiceManager> sm = defaultServiceManager();  
  18.     int ret=ThinkingService::Instance();  
  19.     ProcessState::self()->startThreadPool();  
  20.     IPCThreadState::self()->joinThreadPool();  
  21.     return 0;  
  22. }  

Android.mk
[python] view plain copy
  1. LOCAL_PATH:=$(call my-dir)  
  2. include $(CLEAR_VARS)  
  3. LOCAL_SRC_FILES:=ThinkingServer.cpp  
  4. LOCAL_SHARED_LIBRARIES:=libutils libbinder libThinkingService  
  5. LOCAL_MODULE_TAGS:=optional  
  6. LOCAL_MODULE:=ThinkingServer  
  7. include $(BUILD_EXECUTABLE)  

------------------------------------------------------------------------------------------------------------------------------------------

client



ThinkingClient.h
[cpp] view plain copy
  1. #ifndef THINKING_CLIENT_H  
  2. #define THINKING_CLIENT_H  
  3.   
  4. namespace android  
  5. {  
  6.     class ThinkingClient  
  7.     {  
  8.         public:  
  9.         int setData(int a,int b);  
  10.         private:  
  11.         static void getThinkingService();  
  12.     };  
  13. }  
  14.   
  15. #endif  
ThinkingClient.cpp

[cpp] view plain copy
  1. #include <binder/IServiceManager.h>  
  2. #include <binder/IPCThreadState.h>  
  3. #include "ThinkingClient.h"  
  4.   
  5. namespace android  
  6. {  
  7.     sp<IBinder> binder;  
  8.       
  9.     int ThinkingClient::setData(int a,int b)  
  10.     {  
  11.         getThinkingService();  
  12.         Parcel data,reply;  
  13.         data.writeInt32(a);  
  14.         data.writeInt32(b);  
  15.         binder->transact(0, data, &reply);  
  16.         int result=reply.readInt32();  
  17.         return result;  
  18.     }  
  19.       
  20.     void ThinkingClient::getThinkingService()  
  21.     {  
  22.         sp<IServiceManager> sm = defaultServiceManager();  
  23.         binder = sm->getService(String16("thinking.svc"));  
  24.         if(binder == 0)  
  25.             return;  
  26.     }  
  27. }  

Android.mk
[python] view plain copy
  1. LOCAL_PATH:=$(call my-dir)  
  2. include $(CLEAR_VARS)  
  3. LOCAL_SRC_FILES:=ThinkingClient.cpp  
  4. LOCAL_SHARED_LIBRARIES:=libutils libbinder  
  5. LOCAL_MODULE_TAGS:=optional  
  6. LOCAL_MODULE:=libThinkingClient  
  7. LOCAL_PRELINK_MODULE:=false  
  8. include $(BUILD_SHARED_LIBRARY)  

------------------------------------------------------------------------------------------------------------------------------------------

test




test.cpp

[cpp] view plain copy
  1. #include <stdio.h>  
  2. #include "../client/ThinkingClient.h"  
  3.   
  4. using namespace android;  
  5.   
  6. int main(int argc, char** argv)  
  7. {  
  8.     ThinkingClient client;  
  9.     int result=client.setData(1,2);  
  10.     printf("result is %d \n",result);  
  11.     return 0;  
  12. }  

Android.mk
[python] view plain copy
  1. LOCAL_PATH:=$(call my-dir)  
  2. include $(CLEAR_VARS)  
  3. LOCAL_SRC_FILES:=test.cpp  
  4. LOCAL_SHARED_LIBRARIES:=libThinkingClient  
  5. LOCAL_MODULE_TAGS:=optional  
  6. LOCAL_MODULE:=ThinkingTest  
  7. include $(BUILD_EXECUTABLE)  



------------------------------------------------------------------------------------------------------------------------------------------

整个项目的Android.mk文件(即android-7-0-0_r6\frameworks\native\services\thinking_test\Android.mk)

[python] view plain copy
  1. include $(call all-subdir-makefiles)  
------------------------------------------------------------------------------------------------------------------------------------------

实际验证

1、把这4个部分编译出来

[yong@localhost android-7-0-0_r6]$ cd frameworks/native/services/thinking_test
[yong@localhost thinking_test]$ mm

[yong@localhost android-7-0-0_r6]$ ls out/target/product/angler/system/lib
可以看到
[yong@localhost android-7-0-0_r6]$ ls out/target/product/angler/system/bin
可以看到


2、用adb命令将编译结果push到测试手机里面去

Z:\AndroidSourceCode\android-7-0-0_r6>adb push .\out\target\product\angler\system\lib\libThinkingClient.so /system/lib/
[100%] /system/lib/libThinkingClient.so


Z:\AndroidSourceCode\android-7-0-0_r6>adb push .\out\target\product\angler\system\lib\libThinkingService.so /system/lib/
[100%] /system/lib/libThinkingService.so

Z:\AndroidSourceCode\android-7-0-0_r6>adb push .\out\target\product\angler\system\bin\ThinkingServer /system/bin/
[100%] /system/bin/ThinkingServer


Z:\AndroidSourceCode\android-7-0-0_r6>adb push .\out\target\product\angler\system\bin\ThinkingTest /system/bin/
[100%] /system/bin/ThinkingTest

3、测试
Z:\AndroidSourceCode\android-7-0-0_r6>adb shell
angler:/system/bin # ./ThinkingServer &
[1] 16230
angler:/system/bin # ThinkingService start register
angler:/system/bin # ./ThinkingTest
result is 2
angler:/system/bin #

编译优化

如果嫌mm的方式麻烦
可在android-7-0-0_r6\build\target\product\base.mk添加如下代码
[python] view plain copy
  1. PRODUCT_PACKAGES += \  
  2. ......  
  3.     libThinkingService \  
  4.     libThinkingClient \  
  5.     ThinkingServer \  
  6.     ThinkingTest \  
  7. ......  
即可将这些模块加入AOSP编译系统,这样直接make,就可得到包含这些模块的输出结果(system.img)

阅读全文
0 0
原创粉丝点击