android 新增native binder service 方式(三)

news/2025/2/26 17:13:52

书接上回,继续第三种方式,是手动生成 service binder 的方法,项目结构

1,编译aidl

aidl 文件保持不变,如何生成Bn和Bp 文件呢。

aidl -I ./libserviceaidl/aidl -h ./ -o ./ --lang=cpp libserviceaidl/aidl/com/test/IService.aidl

aidl -I ./libserviceaidl/aidl -h ./ -o ./ --lang=cpp libserviceaidl/aidl/com/test/IServiceCallback.aidl

  -I aidi 文件目录  -h头文件生成路径  -o cpp文件存放路径

2,Native service 差别不大

#define LOG_TAG "testservverfirst"
#include <log/log.h>

#include <unistd.h>
#include <stdlib.h>
#include <utils/RefBase.h>
#include <utils/Log.h>
#include <binder/TextOutput.h>
#include <binder/IInterface.h>
#include <binder/IBinder.h>
#include <binder/ProcessState.h>
#include <binder/IServiceManager.h>
#include <binder/IPCThreadState.h>
#include <termios.h> 
#include <utils/String16.h>
#include "com/test/BnService.h"
#include "com/test/BnServiceCallback.h"


using namespace std;
using namespace android;

class TestService : public com::test::BnService 
{
    public:

    TestService(){
    }

    binder::Status nativecall(int i) 
    {
        ALOGI("server nativecall function is running %d",i);
        if(mycallback != nullptr){
            mycallback->onEventString(4,String16("callbacksuccuess"));
        }
        
        return binder::Status();
    }

    binder::Status  callMcu(int32_t file,const vector<uint8_t>& type, int32_t* _aidl_return){
        *_aidl_return = 1;
        ALOGI("server callMcu function is running _aidl_return",*_aidl_return);
        return binder::Status();
    }

    binder::Status  call(int32_t type){
        ALOGI("server call function is running call %d",type);
        return binder::Status();
    }


    // 实现回调注册
    binder::Status registerCallback(const sp<com::test::IServiceCallback>& callback){
        mycallback  = callback;
        return binder::Status();
    }

    private:
        sp<::com::test::IServiceCallback> mycallback;
    
};

int main(int argc, char const *argv[])
{
    ALOGD("Server is runing");
    defaultServiceManager()->addService(String16("TestService"), new TestService());
    ProcessState::self()->startThreadPool();
    IPCThreadState::self()->joinThreadPool();
    
    return 0;
}

C++ 调用前面有介绍,最后补充下java 的调用方式。

3,Java 调用

try {
    Class<?> clazz = Class.forName("android.os.ServiceManager");

    Method method = clazz.getMethod("getService",String.class);

    IBinder binder = (IBinder) method.invoke(null,"TestService");

    if (binder != null){
        IService iservice = IService.Stub.asInterface(binder);
        // 千万不要忘记注册
        iservice.registerCallback(mycallback);
        iservice.call(1);
    }
} catch (ClassNotFoundException | NoSuchMethodException e) {
    e.printStackTrace();
} catch (IllegalAccessException e) {
    e.printStackTrace();
} catch (InvocationTargetException e) {
    e.printStackTrace();
} catch (RemoteException e) {
    e.printStackTrace();
}

或者系统里面

 IService.Stub.asInterface(ServiceManager.getService("TestService"));

参考

[Android AIDL系列 1] 手动编译aidl文件,生成Java、C++[android]、C++[ndk]、Rust接口_aidl c++-CSDN博客


http://www.niftyadmin.cn/n/5868972.html

相关文章

【LeetCode 热题100】 240. 搜索二维矩阵 II的算法思路及python代码

240. 搜索二维矩阵 II 编写一个高效的算法来搜索 m n m \times n mn 矩阵 m a t r i x matrix matrix 中的一个目标值 t a r g e t target target。该矩阵具有以下特性&#xff1a; 每行的元素从左到右升序排列。每列的元素从上到下升序排列。 示例 1&#xff1a; 输入…

4*A100 部署 deepseek-r1-671B

部署deepseek-r1-671B 使用 4*A100 部署 deepseek-r1-671b-1.58bit 大模型。 环境 ubuntu22.04LTScuda 12.2.0 要求 内存&#xff1a; 256GB及以上显存&#xff1a; 256GB及以上&#xff08;160G可以跑起来&#xff0c;但对于长上下文容易oom&#xff09;&#xff0c;这里…

在 compare-form.vue 中添加 compareDate 隐藏字段,并在提交时自动填入当前时间

在 compare-form.vue 中添加 compareDate 隐藏字段&#xff0c;并在提交时自动填入当前时间。 提交表单时存入的对象是FakeRegistration&#xff0c;这个对象里面有compareDate字段&#xff0c;刚好表格查询的对象也是FakeRegistration&#xff0c;所以表格展示的时间就是刚才…

Cramér-Rao界:参数估计精度的“理论底线”

Cramr-Rao界&#xff1a;参数估计精度的“理论底线” 在统计学中&#xff0c;当我们用数据估计一个模型的参数时&#xff0c;总希望估计结果尽可能精确。但精度有没有一个理论上的“底线”呢&#xff1f;答案是有的&#xff0c;这就是Cramr-Rao界&#xff08;Cramr-Rao Lower …

探秘分布式 IO 耦合器之高速背板总线

在工业自动化的复杂网络中&#xff0c;分布式 IO 耦合器扮演着关键角色&#xff0c;而其中的高速背板总线特性更是核心中的核心&#xff0c;如同一条信息高速公路&#xff0c;让数据传输高效又稳定。 传统的 IO 系统在数据交互时&#xff0c;常常面临数据拥堵、传输延迟等问题&…

Spring源码分析の循环依赖

文章目录 前言一、循环依赖问题二、循环依赖的解决三、整体流程分析 前言 常见的可能存在循环依赖的情况如下&#xff1a; 两个bean中互相持有对方作为自己的属性。   类似于&#xff1a; 两个bean中互相持有对方作为自己的属性&#xff0c;且在构造时就需要传入&#xff1a…

从两地三中心到多地多中心,OceanBase如何实现金融级高可用

“两地三中心”已成为金融领域基准的容灾部署模式。本文将简要阐述金融行业容灾架构中“两地三中心”的具体要求和部署&#xff0c;并进一步探讨OceanBase在实现“两地三中心”标准后&#xff0c;再至“多地多中心”部署中所展现的独特优势与特点。 商业银行的容灾要求 《商业…

智能语音机器人为电销行业带来一场革命性的变化

我们知道一个企业的发展离不开销售&#xff0c;因为它能直接带来盈利&#xff0c;所以很多企业极其重视电话销售这一模式。但随着人工智能技术的发展&#xff0c;许多企业都采用了智能语音机器人来替代人工&#xff0c;甚至替代了传统呼叫中心运/营模式&#xff0c;在极大提升企…