The IInterface, BpInterface, and BnInterface classes are provided by the Android framework.
We start by defining an interface (think AIDL) that will be shared between the service and the client:
| 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 
 classIDemo : publicIInterface { 
    public: 
        enum{ 
            ALERT = IBinder::FIRST_CALL_TRANSACTION, 
            PUSH, 
            ADD 
        }; 
         
        virtualvoidpush(int32_t data)          = 0; 
         
        virtualvoidalert()                     = 0; 
         
        virtualint32_t     add(int32_t v1, int32_t v2) = 0; 
 
        DECLARE_META_INTERFACE(Demo); 
}; 
 
 
IMPLEMENT_META_INTERFACE(Demo, "Demo"); | 
 
 
Next we define the server end, which is made up of 2 classes: BnDemo, and its derived class, Demo. BnDemo extracts the arguments from the data Parcel sent by the client, calls the appropriate virtual function (implemented in the Demo class) to do the heavy-lifting, and packs the returned values (if any) into a reply Parcel to be sent back to the client.
| 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 
classBnDemo : publicBnInterface { 
    virtualstatus_t onTransact(uint32_t code, constParcel& data, 
                                Parcel* reply, uint32_t flags = 0); 
}; 
 
status_t BnDemo::onTransact(uint32_t code, constParcel& data, 
                            Parcel* reply, uint32_t flags) { 
 
    data.checkInterface(this); 
 
    switch(code) { 
        caseALERT: { 
            alert(); 
            returnNO_ERROR; 
        } break; 
        casePUSH: { 
            int32_t inData = data.readInt32(); 
            push(inData); 
            returnNO_ERROR; 
        } break; 
        caseADD: { 
            int32_t inV1 = data.readInt32(); 
            int32_t inV2 = data.readInt32(); 
            int32_t sum = add(inV1, inV2); 
            reply->writeInt32(sum); 
            returnNO_ERROR; 
        } break; 
        default: 
            returnBBinder::onTransact(code, data, reply, flags); 
    } 
} | 
 
 
This is the Demo class, which would normally do the real work on the service side of the binder.
| 
 | 
classDemo : publicBnDemo { 
    virtualvoidpush(int32_t data) { 
         
    } 
    virtualvoidalert() { 
         
    } 
    virtualint32_t add(int32_t v1, int32_t v2) { 
        returnv1 + v2; 
    } 
}; | 
 
 
Now we define a service proxy, to be used on the client side. Notice again that any data the client needs to send to the service is packed in a Parcel and results (if any) are also returned in a Parcel.
| 
 | 
classBpDemo : publicBpInterface { 
    public: 
        BpDemo(constsp& impl) : BpInterface(impl) { } 
 
        virtualvoidpush(int32_t push_data) { 
            Parcel data, reply; 
            data.writeInterfaceToken(IDemo::getInterfaceDescriptor()); 
            data.writeInt32(push_data); 
            remote()->transact(PUSH, data, &reply); 
        } 
 
        virtualvoidalert() { 
            Parcel data, reply; 
            data.writeInterfaceToken(IDemo::getInterfaceDescriptor()); 
            remote()->transact(ALERT, data, &reply, IBinder::FLAG_ONEWAY); 
        } 
 
        virtualint32_t add(int32_t v1, int32_t v2) { 
            Parcel data, reply; 
            data.writeInterfaceToken(IDemo::getInterfaceDescriptor()); 
            data.writeInt32(v1); 
            data.writeInt32(v2); 
            remote()->transact(ADD, data, &reply); 
 
            int32_t res; 
            status_t status = reply.readInt32(&res); 
            returnres; 
        } 
}; | 
 
 
Finally, we start the service as follows:
| 
 
 | 
defaultServiceManager()->addService(String16("Demo"), newDemo()); 
android::ProcessState::self()->startThreadPool(); | 
 
 
And the client can now connect to the service and call some of the provided functions:
| 
 | 
sp sm = defaultServiceManager(); 
sp binder = sm->getService(String16("Demo")); 
sp demo = interface_cast(binder); 
 
demo->alert(); 
demo->push(65); 
int32_t sum = demo->add(453, 827); | 
 
 
Author:Gabriel Burca
Source: 
http://ebixio.com/blog/2012/07/07/using-android-ipc-binders-from-native-code/