Android Native binder 示例

Test_Client.cpp

//
// Created by fukaiqiang on 22-4-7.
//
#include    
#include    
#include    
#include    
#include    
#include    
#include    
using namespace android;
#define SERVER_SERVICE_NAME "test_server"
#define  HELLO_WORLD 1
int main()
{
    sp sm = defaultServiceManager();
    sp iBinder = sm -> getService(String16(SERVER_SERVICE_NAME));
    if (iBinder == NULL)
    {
        ALOGW("server binder service does not exist.");
        return -1;
    }
    Parcel _data,_reply;
    int ret = iBinder -> transact(HELLO_WORLD,_data,&_reply,0);
    ALOGD("client transact ret = %d",ret);
    ProcessState::self()->startThreadPool();
    IPCThreadState::self()->joinThreadPool();
    return 0;
}

Test_Server.cpp

//
// Created by fukaiqiang on 22-4-7.
//
#include    
#include    
#include    
#include    
#include    
#include    
using namespace android;
#define SERVER_SERVICE_NAME "test_server"
#define  HELLO_WORLD 1
class TestServerService:public BBinder
{
public :
    TestServerService()
    {
        descriptor = String16(SERVER_SERVICE_NAME);
    }
    ~TestServerService() {};
    const String16& getInterfaceDescriptor() const
    {
        return descriptor;
    }
    void hello_world()
    {
        ALOGE("service fun hello_world has been called");
    }
    status_t onTransact(uint32_t code,const Parcel& data,
                        Parcel* reply,uint32_t flags = 0)
    {
        ALOGD("service onTransact code = %d", code);
        switch (code) {
            case HELLO_WORLD:
                hello_world();
                break;
            default:
                return BBinder::onTransact(code,data,reply,flags);
        }
        return 0;
    }

private:
    String16 descriptor;
};

int main() {
    sp  sm = defaultServiceManager();
    status_t ret = sm->addService(String16(SERVER_SERVICE_NAME), new TestServerService());
    ALOGD("%s binder service addService ret = %d", SERVER_SERVICE_NAME, ret);
    ProcessState::self()->startThreadPool();
    IPCThreadState::self()->joinThreadPool();
}

server_binder.rc

service server_binder /system/bin/server_binder
    class core

client_binder.rc

service client_binder /system/bin/client_binder
    class core

Android.bp

cc_binary {
    name: "server_binder",
    srcs: [
        "Test_Server.cpp",
    ],
    shared_libs: [
        "libcutils",
        "libbinder",
        "libutils",
        "liblog"
    ],
    init_rc: ["server_binder.rc"],
}
cc_binary {
    name: "client_binder",
    srcs: [
        "Test_Client.cpp",
    ],
    shared_libs: [
        "libcutils",
        "libbinder",
        "libutils",
        "liblog"
    ],
    init_rc: ["client_binder.rc"],
}

注意

Android如果默认打开selinux,需要关闭selinux,才能在android系统上执行调用。至于添加selinux部分,这不是重点,这就不再进行添加了,其实也简单,按照日志提示添加即可。

结果示例

D         : test_server binder service addService ret = 0
D         : service onTransact code = 1
E         : service fun hello_world has been called

调用过程后面再分析。

你可能感兴趣的:(Android Native binder 示例)