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

2012年(1)

2011年(14)

2010年(14)

我的朋友

分类: 嵌入式

2010-12-21 16:17:43

自定义Android核心服务实践

Daniel Wood 20101203

转载时请注明出处和作者

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

作者:Daniel Wood

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

3.    CalServiceBp端实现

完成对CalService服务的封装,提供JNI对服务的调用。

Cal.h文件

#ifndef ANDROID_CAL_H
#define ANDROID_CAL_H
#include <utils/RefBase.h>
#include <binder/IInterface.h>
#include <binder/Parcel.h>

namespace android
{
    class Cal
    {
        public:
            int Add(int a, int b);
            int Sub(int a, int b);
            int Mul(int a, int b);
            float Div(int a, int b);

        private:
            static const void getCalService();
    };
};
#endif

Cal.cpp文件

#include <binder/IServiceManager.h>
#include <binder/IPCThreadState.h>

#include "Cal.h"
#include "../libcalservice/CalService.h"
/*包含CalService.h文件主要是要得到操作命令类型:ADD,SUB,MUL,DIV*/
namespace android
{
    sp<IBinder> binder;
    int Cal::Add(int a, int b)
    {
        getCalService();
        Parcel data, reply;
        data.writeInt32(getpid());
        data.writeInt32(a);
        data.writeInt32(b);
        LOGD("BpCalService::create remote()->transact()\n");
        binder->transact(CalService::ADD, data, &reply);
        int result = reply.readInt32();
        return result;
    }
    int Cal::Sub(int a, int b)
    {
        getCalService();
        Parcel data, reply;
        data.writeInt32(getpid());
        data.writeInt32(a);
        data.writeInt32(b);
        LOGD("BpCalService::create remote()->transact()\n");
        binder->transact(CalService::SUB, data, &reply);
        int result = reply.readInt32();
        return result;
    }
    int Cal::Mul(int a, int b)
    {
        getCalService();
        Parcel data, reply;
        data.writeInt32(getpid());
        data.writeInt32(a);
        data.writeInt32(b);
        LOGD("BpCalService::create remote()->transact()\n");
        binder->transact(CalService::MUL, data, &reply);
        int result = reply.readInt32();
        return result;
    }
    float Cal::Div(int a, int b)
    {
        getCalService();
        Parcel data, reply;
        data.writeInt32(getpid());
        data.writeInt32(a);
        data.writeInt32(b);
        LOGD("BpCalService::create remote()->transact()\n");
        binder->transact(CalService::DIV, data, &reply);
        float result = reply.readFloat();
        return result;
    }
    /*获取CalService服务*/
    const void Cal::getCalService()
    {
        sp<IServiceManager> sm = defaultServiceManager();
        binder = sm->getService(String16("CalService"));
//这里的服务名必须和注册服务时相同的服务名才可以得到相应服务
        if(binder == 0 )
        {
            LOGD("CalService not published, getCalService Fail");
            return ;
        }
    }
};

Android.mk文件

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:=Cal.cpp
LOCAL_C_INCLUDES:= $(JNI_H_INCLUDE) \
    frameworks/base/include
LOCAL_SHARED_LIBRARIES:= \
libCalService \
 libutils \
 libcutils \
 libbinder \
 libandroid_runtime
LOCAL_MODULE := libCal    #模块名字
LOCAL_PRELINK_MODULE:= false
include $(BUILD_SHARED_LIBRARY)

通过make libCal命令编译模块:

target thumb C++: libCal <= frameworks/base/myservice/libcal/Cal.cpp
In file included from frameworks/base/myservice/libcal/Cal.cpp:4:
target SharedLib: libCal (out/target/product/generic/obj/SHARED_LIBRARIES/libCal_intermediates/LINKED/libCal.so)
target Non-prelinked: libCal (out/target/product/generic/symbols/system/lib/libCal.so)
target Strip: libCal (out/target/product/generic/obj/lib/libCal.so)
Install: out/target/product/generic/system/lib/libCal.so


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