当前位置: 首页 > news >正文

android 新增native binder service 方式(三)

书接上回,继续第三种方式,是手动生成 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博客

相关文章:

  • 系统思考:第五项修炼
  • 『obsidian』obsidian接入DeepSeek模型的完整说明
  • 神经网络 - 神经元
  • LangChain教程 - RAG - 支持的100种向量数据库
  • 九、数据治理架构流程
  • JSON 简介、语法、使用场景与 JavaScript 操作指南
  • 【时时三省】(C语言基础)常量和变量
  • 一文读懂:AIGC 在工业互联网产教融合实训平台的创新应用
  • 05-1基于vs2022的c语言笔记——运算符
  • sklearn中的决策树-分类树:实例-分类树在合成数据集上的表现
  • NVIDIA DLI引领创新课程:基于提示工程的LLM应用开发探索
  • Bybit最大资金盗窃事件技术分析 by CertiK
  • 递归(典型算法思想)—— OJ例题算法解析思路
  • R-INLA实现绿地与狐狸寄生虫数据空间建模:含BYM、SPDE模型及PC先验应用可视化...
  • Java使用EasyExcel实现异步导出
  • JavaScript web APIs第一天——04-code——06-随机抽奖案例.html
  • 爱普生汽车用显示控制器IC:ScalerIC,汽车接口IC,相机接口IC
  • 如何使用tushare pro获取股票数据——附爬虫代码以及tushare积分获取方式
  • 编写一个程序,输入一个字符串并输出其长度(Python版)
  • 网络安全 机器学习算法 计算机网络安全机制
  • 对话作家吉井忍:“滚石”般的生活,让我看到多种人生可能
  • 谷神星一号海射型遥五运载火箭发射成功
  • 浦江潮涌征帆劲,上海以高质量发展服务全国发展大局
  • 意德首脑会谈,梅洛尼警告欧盟绿色政策面临“工业荒漠化”
  • 广东高州发生山体滑坡,造成2人遇难4人送医救治1人失联
  • 多图|多款先进预警机亮相雷达展,专家:中国预警机已达世界先进水平