Example analysis of Android4.1

  • 2020-09-28 09:09:31
  • OfStack

This article illustrates the use of BinderService in Android4.1. To share for your reference, the details are as follows:

A new class, BinderService, appears in Android4.1, and all Native Service inherit from this class.


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;
  }
};

As you can see from the code, publish() of this class registers Native Service to ServiceManager, and BinderService is the friend metaclass of NativeService. This is because BinderService needs to access the getServiceName method of Native Service.

I hope this article has been helpful in Android programming.


Related articles: