Android Service Framework

来源:互联网 发布:酷彩 菜谱 知乎 编辑:程序博客网 时间:2024/04/28 02:12

使用的版本:Android 2.3

Android Service Framework:是一系列类的集合,用来开发运行在Android平台上的各种服务。要格外注意服务框架与应用程序框架(Application Framework)的区别。在使用应用程序框架时,由Java实现的服务将被包含进去,而C++实现的服务不会被包含进去,所以在开发服务时需要一个通用的术语来指代所使用的框架。Java Service Framework与Native Service Framework合起来构成Android Service Framework,其中Java Service Framework通过JNI本地函数与Native Service Framework进行交互。
Android Service Framework提供4种主要功能。
1.服务接口:在服务接口中以函数的形式声明服务要提供的功能,而后服务或服务代理实现该接口中的服务函数,提供接口中定义的一系列服务。
2.服务生成:服务生成功能能支持服务与服务代理的生成。
3.Binder IPC处理:Binder IPC处理功能提供Binder IPC的生成以及与Binder Driver通信的功能,以便支持服务与服务使用者之间的Binder IPC通信。
4.服务管理(Service Manager):提供向系统注册或检索服务的功能。


1.服务框架的构成

Service Framework分三层:服务层、RPC层、IPC层。
在服务层:Service Client有服务接口,Service Server不仅有服务接口,还有相应的服务。
在RPC层:Service Client有服务代理,Service Service中有服务Stub。
在IPC层:Service Client与Service Server的构成元素都是一样的。
位于服务层中的服务接口的Client和Server使用统一的接口进行相互作用,它们有统一的接口。在RPC层的服务代理与服务Stub支持Binder RPC操作,服务代理将函数调用信息转换为RPC代码和数据。Server Stub分析服务代理发送来的RPC代码与数据,然后调用服务Stub函数。位于IPC层的BpBinder类与BBinder类支持Binder IPC操作。在Binder IPC中,当调用BpBinder的transact()函数时,BBinder的transact()函数即被调用,同时传递RPC代码与数据。若服务函数不是Service Framework提供的基本函数,则需要重新定义BBinder的OnTransact()函数,具体实现Service Stub类,以便调用Service Stub函数。
这里是BpBinder的头文件,位于/frameworks/base/include/binder目录中,源文件位于/framework/base/libs/binder目录中
#ifndef ANDROID_BPBINDER_H#define ANDROID_BPBINDER_H#include <binder/IBinder.h>#include <utils/KeyedVector.h>#include <utils/threads.h>// ---------------------------------------------------------------------------namespace android {class BpBinder : public IBinder{public:                        BpBinder(int32_t handle);    inline  int32_t     handle() const { return mHandle; }    virtual const String16&    getInterfaceDescriptor() const;    virtual bool        isBinderAlive() const;    virtual status_t    pingBinder();    virtual status_t    dump(int fd, const Vector<String16>& args);    virtual status_t    transact(   uint32_t code,                                    const Parcel& data,                                    Parcel* reply,                                    uint32_t flags = 0);    virtual status_t    linkToDeath(const sp<DeathRecipient>& recipient,                                    void* cookie = NULL,                                    uint32_t flags = 0);    virtual status_t    unlinkToDeath(  const wp<DeathRecipient>& recipient,                                        void* cookie = NULL,                                        uint32_t flags = 0,                                        wp<DeathRecipient>* outRecipient = NULL);    virtual void        attachObject(   const void* objectID,                                        void* object,                                        void* cleanupCookie,                                        object_cleanup_func func);    virtual void*       findObject(const void* objectID) const;    virtual void        detachObject(const void* objectID);    virtual BpBinder*   remoteBinder();            status_t    setConstantData(const void* data, size_t size);            void        sendObituary();    class ObjectManager    {    public:                    ObjectManager();                    ~ObjectManager();        void        attach( const void* objectID,                            void* object,                            void* cleanupCookie,                            IBinder::object_cleanup_func func);        void*       find(const void* objectID) const;        void        detach(const void* objectID);        void        kill();    private:                    ObjectManager(const ObjectManager&);        ObjectManager& operator=(const ObjectManager&);        struct entry_t        {            void* object;            void* cleanupCookie;            IBinder::object_cleanup_func func;        };        KeyedVector<const void*, entry_t> mObjects;    };protected:    virtual             ~BpBinder();    virtual void        onFirstRef();    virtual void        onLastStrongRef(const void* id);    virtual bool        onIncStrongAttempted(uint32_t flags, const void* id);private:    const   int32_t             mHandle;    struct Obituary {        wp<DeathRecipient> recipient;        void* cookie;        uint32_t flags;    };            void                reportOneDeath(const Obituary& obit);            bool                isDescriptorCached() const;    mutable Mutex               mLock;            volatile int32_t    mAlive;            volatile int32_t    mObitsSent;            Vector<Obituary>*   mObituaries;            ObjectManager       mObjects;            Parcel*             mConstantData;    mutable String16            mDescriptorCache;};}; // namespace android// ---------------------------------------------------------------------------#endif // ANDROID_BPBINDER_H
IBinder类是对Android Binder的抽象,它有BBinder和BpBinder两个子类、BBinder类负责接收RPC代码和数据,并在Binder Driver内部生成Binder节点。BpBinder类保存着目标服务的Handle信息,用于Binder Driver查找Service Server的Binder节点的过程中。
Iinterface类提供类型变换的功能,将服务或服务代理类转换为IBindr类型。实际的类型转换是由BnInterface、BpInterface两个类完成,BnInterface将服务类转换成IBinder类型,而BpInterface则将服务代理类转换成IBinder类型。在通过Binder Driver传递Binder对象时,必须进行类型转换,比如向系统注册服务时,需要先将服务类转换成IBinder,再将其传递给Context Manager。
ProcessState类用来管理 Binder Driver,IPCThreadState类用来支持服务客户端、Service Server与Binder Driver间的Binder IPC通信。
Parcel是在服务与服务代理间进行Binder IPC时,保存Binder IPC数据。Parcel类可以处理的数据有C语言的基本数据结构、基本数据结构数组、Binder对象、文件描述符等。

2.运行机制

这里拿AudioFlinger作为例子。IAudioFlinger继承IInterface类,IInterface类的主要功能之一就是支持服务与IBinder类型间的显式转换。IInterface类通过它自己的asBinder()函数将IAudioFlinger类型转换为IBinder类型。如在向系统注册Audio Flinger服务时,Service Manager首先要将BBinder类型的服务对象包含到RPC数据中,再将其传递给Context Manager,在这一过程中需要先把服务转换成IBinder类型,在传递给Binder Driver。看一下asBinder()函数的实现代码。
sp<IBinder> IInterface::asBinder(){    return this ? onAsBinder() : NULL;}sp<const IBinder> IInterface::asBinder() const{    return this ? const_cast<IInterface*>(this)->onAsBinder() : NULL;}
这是个虚函数,子类中必须实现该虚函数。BnInterface与BpInterface类继承了IAudioFlinger类,所以这两个类的OnAsBinder()函数必须要被实现。看一下这IAudioFlinger的DECLARE_META_INTERFACE宏
#define DECLARE_META_INTERFACE(INTERFACE)                               \    static const android::String16 descriptor;                          \    static android::sp<I##INTERFACE> asInterface(                       \            const android::sp<android::IBinder>& obj);                  \    virtual const android::String16& getInterfaceDescriptor() const;    \    I##INTERFACE();                                                     \    virtual ~I##INTERFACE();                                            \

这个宏会被预编译为asInterface()函数。

0 0
原创粉丝点击