Chinaunix首页 | 论坛 | 博客
  • 博客访问: 228663
  • 博文数量: 29
  • 博客积分: 1477
  • 博客等级: 上尉
  • 技术积分: 451
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-06 14:48
文章分类
文章存档

2012年(1)

2011年(14)

2010年(14)

我的朋友

分类: 嵌入式

2010-12-21 15:54:25

自定义Android核心服务实践

Daniel Wood 20101203

转载时请注明出处和作者

文章出处:http://danielwood.cublog.cn

作者:Daniel Wood

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

1.    CalService实现

CalService.h文件

#ifndef ANDROID _CAL_SERVICE_H
#define ANDROID _CAL_SERVICE_H
#include <utils/RefBase.h>
#include <binder/IInterface.h>
#include <binder/Parcel.h>
namespace android
{
    class CalService : public BBinder
    {
    public:
     enum {    //运算操作的命令类型
        ADD,
        SUB,
        MUL,
        DIV,
     };
            static int instantiate();
            CalService();
            virtual ~CalService();
            virtual status_t onTransact(uint32_t, const Parcel&, Parcel*, uint32_t);
    };
};
#endif


  在CalService.h中定义了计算器服务提供的计算操作类型,以及CalService服务继承BBinder类。

CalService.cpp文件

#include "CalService.h"
#include <binder/IServiceManager.h>
#include <binder/IPCThreadState.h>
namespace android
{
       static struct sigaction oldact;
       static pthread_key_t sigbuskey;

       int CalService::instantiate()
       {
     LOGD("CalService instantiate");
/*向ServiceManager注册CalService服务,名字为"CalService"*/
           int ret = defaultServiceManager()->addService(String16("CalService"), new CalService());
           return ret;
       }

       CalService::CalService()
       {
     LOGD("CalService created");
           pthread_key_create(&sigbuskey, NULL);
       }

       CalService::~CalService()
       {
     pthread_key_delete(sigbuskey);
           LOGD("CalService destroyed");
       }
/*具体实现函数*/
       status_t CalService::onTransact(uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
       {
            switch(code)
     {
                case ADD: {
                              pid_t pid = data.readInt32();
                              int num1 = data.readInt32();
             int num2 = data.readInt32();
                              int result = num1 + num2;
                              reply->writeInt32(result);
                              return NO_ERROR;
                          } break;
        case SUB: {
             pid_t pid = data.readInt32();
                              int num1 = data.readInt32();
                              int num2 = data.readInt32();
             int result = num1 - num2;
                              reply->writeInt32(result);
                              return NO_ERROR;
             } break;
        case MUL: {
             pid_t pid = data.readInt32();
                              int num1 = data.readInt32();
                              int num2 = data.readInt32();
             int result = num1 * num2;
                              reply->writeInt32(result);
                              return NO_ERROR;
             } break;
        case DIV: {
             pid_t pid = data.readInt32();
                              int num1 = data.readInt32();
                              int num2 = data.readInt32();
             float result = num1 / num2;
                              reply->writeFloat(result);
                              return NO_ERROR;
             } break;
                default:
             return BBinder::onTransact(code, data, reply, flags);
              }
       }    
};

CalService.cpp中实现了服务的onTransact方法,以及注册服务的instantiate方法。注意注册时用的服务名字,在后面使用服务的时候名字要相同。

Android.mk文件

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= CalService.cpp
LOCAL_C_INCLUDES:= $(JNI_H_INCLUDE) \
    frameworks/base/include #注意添加头文件路径
LOCAL_SHARED_LIBRARIES:= \  #注意需要调用这些共享库
 libutils \
 libcutils \
 libbinder \
 libandroid_runtime
LOCAL_MODULE:= libCalService #服务的库名字
LOCAL_PRELINK_MODULE:= false
include $(BUILD_SHARED_LIBRARY)

可以通过make libCalService命令编译CalService模块。模块编译的输出结果:

target thumb C++: libCalService <= frameworks/base/myservice/libcalservice/CalService.cpp
target SharedLib: libCalService (out/target/product/generic/obj/SHARED_LIBRARIES/libCalService_intermediates/LINKED/libCalService.so)
target Non-prelinked: libCalService (out/target/product/generic/symbols/system/lib/libCalService.so)
target Strip: libCalService (out/target/product/generic/obj/lib/libCalService.so)
Install: out/target/product/generic/system/lib/libCalService.so

out/target/product/generic/system/lib目录下面生成了libCalService.so文件。

阅读(2443) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~