其他分享
首页 > 其他分享> > BinderService模板类

BinderService模板类

作者:互联网

binder服务类通过继承BinderService模板类,可以方便的发布binder服务到servicemanager。

template<typename SERVICE>
class BinderService
{
public:
    static status_t publish(bool allowIsolated = false) {
        sp<IServiceManager> sm(defaultServiceManager());
        return sm->addService(
                String16(SERVICE::getServiceName()),
                new SERVICE(), allowIsolated);
    }

    static void publishAndJoinThreadPool(bool allowIsolated = false) {
        publish(allowIsolated);
        joinThreadPool();
    }

    static void instantiate() { publish(); }

    static status_t shutdown() { return NO_ERROR; }
private:
    static void joinThreadPool() {
        sp<ProcessState> ps(ProcessState::self());
        ps->startThreadPool();
        ps->giveThreadPoolName();
        IPCThreadState::self()->joinThreadPool();
    }
};

BinderService模板类用法
class AudioFlinger :
    public BinderService<AudioFlinger>,
    public BnAudioFlinger
{
    friend class BinderService<AudioFlinger>;   // for AudioFlinger()

public:
    static const char* getServiceName() ANDROID_API { return "media.audio_flinger"; }
	...
};

// main_mediaserver.cpp
int main() {
    ...  
    AudioFlinger::instantiate();
    AudioPolicyService::instantiate();
    ...
}

标签:void,instantiate,static,allowIsolated,BinderService,public,模板
来源: https://www.cnblogs.com/bobfly1984/p/16367198.html