当前位置: 首页 > 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博客

http://www.lryc.cn/news/544156.html

相关文章:

  • (IDE接入DeepSeek)简单了解DeepSeek接入辅助开发与本地部署建议
  • seasms v9 注入漏洞 + order by注入+​information_schema​解决方法
  • 【实战 ES】实战 Elasticsearch:快速上手与深度实践-1.3.1单节点安装(Docker与手动部署)
  • 如何使用useEffect模拟组件的生命周期?
  • 【DeepSeek】私有化本地部署图文(Win+Mac)
  • Python 入门教程(2)搭建环境 | 2.3、VSCode配置Python开发环境
  • Wireshark详解
  • 《从零开始掌握Python:一份全面的学习指南》
  • 布署elfk-准备工作
  • LlamaFactory-webui:训练大语言模型的入门级教程
  • 达梦数据库授权给某个用户查询其他指定用户下所有表的权限
  • uniapp 微信小程序打包之后vendor.js 主包体积太大,解决办法,“subPackages“:true设置不生效
  • Docker数据卷容器实战
  • 【Eureka 缓存机制】
  • docker-compose方式启动Kafka Sasl加密认证(无zk)
  • [ComfyUI]官方已支持Skyreels混元图生视频,速度更快,效果更好(附工作流)
  • 数据库导出
  • Flask 应用结构与模块化管理详细笔记
  • Excel的两个小问题解决
  • 计算机毕业设计Python+DeepSeek-R1大模型期货价格预测分析 期货价格数据分析可视化预测系 统 量化交易大数据 机器学习 深度学习
  • JVM 面试
  • 智慧后勤的消防管理:豪越科技为安全护航
  • 【Elasticsearch】(Java 版)
  • DeepSeek在昇腾上的模型部署 - 常见问题及解决方案
  • 安全面试5
  • 【Python量化金融实战】-第2章:金融市场数据获取与处理:2.1 数据源概览:Tushare、AkShare、Baostock、通联数据(DataAPI)
  • Exoplayer(MediaX)实现音频变调和变速播放
  • 服务器间迁移conda环境
  • docker高级
  • Redis Stream基本使用及应用场景