android 新增native binder service 方式(三)

devtools/2025/3/1 2:11:17/

书接上回,继续第三种方式,是手动生成 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.ppmy.cn/devtools/163498.html

相关文章

【react】TypeScript在react中的使用

目录 一、环境与项目配置 1. 创建 TypeScript React 项目 2. 关键tsconfig.json配置 3.安装核心类型包 二、组件类型定义 1. 函数组件&#xff08;React 18&#xff09; 2.类组件 三、Hooks 的深度类型集成 1. useState 2. useEffect 3. useRef 4. 自定义 Hook 四、事…

Ollama+Cherrystudio+beg-m3+Deepseek R1 32b部署本地私人知识库(2025年2月win11版)

之前综合网络各方面信息得到的配置表&#xff1a; 在信息爆炸的时代&#xff0c;数据安全和个性化需求愈发凸显。搭建本地私人知识库&#xff0c;不仅能确保数据的安全性&#xff0c;还能根据个人需求进行个性化定制&#xff0c;实现知识的高效管理和利用。随着技术的不断发展…

GDidees CMS v3.9.1本地文件泄露漏洞(CVE-2023-27179)

漏洞简介&#xff1a; GDidees CMS v3.9.1及更低版本被发现存在本地文件泄露漏洞&#xff0c;漏洞通过位于 /_admin/imgdownload.php 的 filename 参数进行利用。 漏洞环境&#xff1a; 春秋云镜中的漏洞靶标&#xff0c;CVE编号为CVE-2023-27179 漏洞复现: 进入靶场发现没…

《Somewhat Practical Fully Homomorphic Encryption》笔记 (BFV 源于这篇文章)

文章目录 一、摘要二、引言1、FHE 一般分为三个逻辑部分2、噪声的管理3. 贡献点4. 文章思路 三、基础数学知识四、基于 RLWE 的加密1. LWE 问题2. RLWE 问题3. RLWE 问题的难度和安全性 五、加密方案1. LPR.ES 加密方案2. Lemma 1 (引理 1)3. Optimisation/Assumption 1 (优化/…

Elasticsearch:使用经过训练的 ML 模型理解稀疏向量嵌入

作者&#xff1a;来自 Elastic Dai Sugimori 了解稀疏向量嵌入&#xff0c;理解它们的作用/含义&#xff0c;以及如何使用它们实现语义搜索。 Elasticsearch 提供语义搜索功能&#xff0c;允许用户使用自然语言进行查询并检索相关信息。为此&#xff0c;目标文档和查询必须首先…

【Mac电脑本地部署Deepseek-r1:详细教程与Openwebui配置指南】

文章目录 前言电脑配置&#xff1a;安装的Deepseek版本&#xff1a;使用的UI框架&#xff1a;体验效果展示&#xff1a;本地部署体验总结 部署过程Ollama部署拉取模型运行模型Openwebui部署运行Ollama服务在Openwebui中配置ollama的服务 后话 前言 deepseek最近火的一塌糊涂&a…

C#装箱拆箱机制详解

在C#中&#xff0c;装箱&#xff08;Boxing&#xff09;和拆箱&#xff08;Unboxing&#xff09; 是值类型与引用类型之间转换的核心机制。它们的实现直接影响程序的性能和类型安全。 一、装箱&#xff08;Boxing&#xff09; 定义&#xff1a; 将值类型转换为引用类型&#…

使用大语言模型(Deepseek)构建一个基于 SQL 数据的问答系统

GitHub代码仓库 架构 从高层次来看&#xff0c;这些系统的步骤如下&#xff1a; 将问题转换为SQL查询&#xff1a;模型将用户输入转换为SQL查询。 执行SQL查询&#xff1a;执行查询。 回答问题&#xff1a;模型根据查询结果响应用户输入。 样本数据 下载样本数据&#xf…