Android4.1中BinderService的作用

来源:互联网 发布:mac 安装linux 编辑:程序博客网 时间:2024/04/26 07:14
Android4.1 中出现了一个新的类,BinderService,所有的Native 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) {
        sp<IServiceManager> sm(defaultServiceManager());
        sm->addService(String16(SERVICE::getServiceName()), new SERVICE(), allowIsolated);
        ProcessState::self()->startThreadPool();
        IPCThreadState::self()->joinThreadPool();
    }

    static void instantiate() { publish(); }

    static status_t shutdown() {
        return NO_ERROR;
    }
};
从代码中可以得知,这个类的publish(),就是将Native Service 注册到ServiceManager,同时 BinderService 作为 NativeService 的友元类。这是因为BinderService 需要访问 Native Service 的getServiceName方法。
原创粉丝点击