美文网首页
Android Native binder 示例

Android Native binder 示例

作者: 付凯强 | 来源:发表于2022-08-15 14:33 被阅读0次

Test_Client.cpp

//
// Created by fukaiqiang on 22-4-7.
//
#include    <binder/IServiceManager.h>
#include    <binder/IBinder.h>
#include    <binder/Parcel.h>
#include    <binder/ProcessState.h>
#include    <binder/IPCThreadState.h>
#include    <private/binder/binder_module.h>
#include    <cutils/log.h>
using namespace android;
#define SERVER_SERVICE_NAME "test_server"
#define  HELLO_WORLD 1
int main()
{
    sp<IServiceManager> sm = defaultServiceManager();
    sp<IBinder> 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    <binder/IServiceManager.h>
#include    <binder/IBinder.h>
#include    <binder/Parcel.h>
#include    <binder/ProcessState.h>
#include    <binder/IPCThreadState.h>
#include    <cutils/log.h>
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 <IServiceManager> 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 示例

      本文链接:https://www.haomeiwen.com/subject/isxlgrtx.html