Search code examples
androidc++android-serviceandroid-binder

How to Create a android native service and use binder to communicate with it?


My basic task is to create a native service in android and then write a simple native program to test it. lets say I want to write a simple service which return me sum of two integers. I have to use binders to talk to it from the program, I have tried to google around but I can't find a precise example. I need to know how to create a native service and find it in the program and if needed in Java also.


Solution

  • The solution that I found is to use the Binders in native and use the

    defaultServiceManager()->addService(
            String16("TestService"),new CalcService());
    

    and then use binders and use following on client side.

    sp<IServiceManager> sm = defaultServiceManager();
    sp<IBinder> binder = sm->getService(String16("TestService"));
    

    I found examples here on how to do this: https://github.com/gburca/BinderDemo/blob/master/binder.cpp