android关于binder的简单通信过程

news/2024/9/18 13:30:45/ 标签: android, binder, aidl

文章目录

  • 简述
  • aidl文件
  • 服务端的实现
  • 客户端的实现
  • 验证过程

简述

主要实现的是两个应用之间跨进程通信的过程,client端调用server端的具体实现,然后server端给client回调数据,详细如下所示

aidl_3">aidl文件

以下的文件需要在服务端与客户端都配置一份且保持一致

1.aidl 跨进程所需要的文件目录如下所示
在这里插入图片描述
以下文件是对应的TestDataBean.aidl文件的
在这里插入图片描述

2.IOnTestDataCallback.aidl文件,用于服务端给客户端回调接口,传递一个parcel类型的数据

// IOnTestDataCallback.aidl
package com.example.test;
import com.example.test.TestDataBean;// Declare any non-default types here with import statementsinterface IOnTestDataCallback {void onCallback(in TestDataBean dataBean);
}

3.IOnTestDataListener.aidl文件,用于客户端给服务端请求数据的调用过程,oneway表示异步方法,不用等待方法中的实现执行完成,直接返回就行了。

// IOnTestDataListener.aidl
package com.example.test;
import com.example.test.IOnTestDataCallback;
// Declare any non-default types here with import statementsinterface IOnTestDataListener {oneway void sendData(String str, in byte[] bytes,in IOnTestDataCallback callback);
}

4.TestDataBean.aidl文件,用于跨进程传递parcel对象数据。

// TestDataBean.aidl
package com.example.test;parcelable TestDataBean;

5.TestDataBean.java文件,TestDataBean.aidl中的具体实现方式。

package com.example.test;import android.os.Parcel;
import android.os.Parcelable;public class TestDataBean implements Parcelable {public String getName() {return name;}public void setName(String name) {this.name = name;}public int getNumber() {return number;}public void setNumber(int number) {this.number = number;}private String name;private int number;public TestDataBean() {}protected TestDataBean(Parcel in) {name = in.readString();number = in.readInt();}@Overridepublic void writeToParcel(Parcel dest, int flags) {dest.writeString(name);dest.writeInt(number);}@Overridepublic int describeContents() {return 0;}public static final Creator<TestDataBean> CREATOR = new Creator<TestDataBean>() {@Overridepublic TestDataBean createFromParcel(Parcel in) {return new TestDataBean(in);}@Overridepublic TestDataBean[] newArray(int size) {return new TestDataBean[size];}};
}

服务端的实现

1.TestBinderService.java文件

package com.tencent.wemeet.testwhitelist;import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;import androidx.annotation.Nullable;import com.example.test.IOnTestDataCallback;
import com.example.test.IOnTestDataListener;
import com.example.test.TestDataBean;import java.util.Arrays;public class TestBinderService extends Service {private static final String TAG = "TestBinderService";@Nullable@Overridepublic IBinder onBind(Intent intent) {return new IOnTestDataListener.Stub() {@Overridepublic void sendData(String str, byte[] bytes, IOnTestDataCallback callback) throws RemoteException {Log.d(TAG, "str = " + str + ",bytes = " + Arrays.toString(bytes));TestDataBean bean = new TestDataBean();bean.setName("zhangsan");bean.setNumber(20);try {Thread.sleep(5000);callback.onCallback(bean);} catch (InterruptedException e) {throw new RuntimeException(e);}}};}@Overridepublic void onCreate() {super.onCreate();Log.d(TAG, "onCreate");}
}

2.AndroidManifest.xml文件配置

        <serviceandroid:name=".TestBinderService"android:exported="true"><intent-filter><action android:name="com.binder.test.service" /></intent-filter></service>

客户端的实现

TestBinderActivity.java文件

package com.example.test;import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;public class TestBinderActivity extends AppCompatActivity {public static final String TAG = "TestBinderActivity";private boolean isBinder;private IOnTestDataListener binder;@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_binder_test);Intent intent = new Intent();intent.setAction("com.binder.test.service");intent.setPackage("com.tencent.wemeet.testwhitelist");isBinder = bindService(intent, connection, Context.BIND_AUTO_CREATE);findViewById(R.id.btn_test).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {if (binder == null) {Log.d(TAG, "binder is null");return;}Log.d(TAG, "begin");try {binder.sendData("123", new byte[]{1, 2, 3}, new IOnTestDataCallback.Stub() {@Overridepublic void onCallback(TestDataBean dataBean) throws RemoteException {Log.d(TAG, "name = " + dataBean.getName() + ",number = " + dataBean.getNumber());}});} catch (RemoteException e) {Log.d(TAG, "sendData RemoteException");throw new RuntimeException(e);}Log.d(TAG, "end");}});}@Overrideprotected void onDestroy() {super.onDestroy();if (isBinder) {isBinder = false;unbindService(connection);connection = null;binder = null;}}private ServiceConnection connection = new ServiceConnection() {@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {Log.d(TAG, "onServiceConnected");binder = IOnTestDataListener.Stub.asInterface(service);}@Overridepublic void onServiceDisconnected(ComponentName name) {Log.d(TAG, "onServiceDisconnected");}};
}

验证过程

先打开server端相关的应用进程,之后再打开client端相关的应用进程,然后通过binder的方式连接server端并进行通信,记录大致的通信过程。


http://www.ppmy.cn/news/1516913.html

相关文章

Python | Linux | 解析Himawari-8/9 | Standard Data

写作前面 之前一个相关的工作需要解析Himawari-8/9 Standard Data文件&#xff0c;因为他是二进制的&#xff0c;之前没有处理过&#xff0c;导致完全摸不着头脑。在网上找了中英文搜索找了好久&#xff0c;虽然也找到了公开的解析代码&#xff0c;但是放在自己的数据这感觉总是…

Vue中使用el-table自定义序号翻页后又从1开始没有连续

在 ​​el-table​​​ 中&#xff0c;自定义序号列在翻页后会重新从 1 开始是因为每页的数据是重新渲染的&#xff0c;没有保留之前的序号。如果您希望在翻页后保持连续的序号&#xff0c;可以使用 ​​index​​ 属性来获取全局的行索引。 以下是一个示例&#xff0c;演示如…

如何在UE5.4中重新定位动画?

动画在游戏和电影制作中扮演着至关重要的角色&#xff0c;而在虚幻引擎5.4&#xff08;UE5.4&#xff09;这一强大的实时3D创作平台中&#xff0c;重新定位动画的能力更是将创意表达推向了新的高度。本文将引导您探索UE5.4中重新定位动画的技巧&#xff0c;确保您的动画作品不仅…

error on “nvm list available“, find the final solution by the Second error

error one Could not retrieve https://nodejs.org/dist/index.json. Get “https://nodejs.org/dist/index.json”: dial tcp 104.20.23.46:443: i/o timeout error two Error retrieving “http://npm.taobao.org/mirrors/node/index.json”: HTTP Status 404 PS D:\code…

基于Python的机器学习系列(13):Bagging

在上一篇文章中&#xff0c;我们讨论了决策树的原理及其在分类任务中的应用。然而&#xff0c;单一的决策树模型往往容易过拟合&#xff0c;导致泛化能力较差。为了减小模型的方差&#xff0c;我们可以通过构建多个决策树来提升模型的表现。这种方法被称为Bagging&#xff08;B…

OPTICS算法原理及Python实践

OPTICS&#xff08;Ordering Points To Identify the Clustering Structure&#xff09;算法是一种基于密度的聚类算法&#xff0c;它的核心思想是通过计算数据点之间的密度关系&#xff0c;自动发现数据中的层次结构&#xff0c;而无需预先设定簇的数量。以下是OPTICS算法原理…

OpenCV入门12.2:SURF与SIFT比较及SURF示例

SIFT (Scale-Invariant Feature Transform): 提出时间: 1999年&#xff0c;由David Lowe提出。关键特点: 能够检测和描述图像中的关键点&#xff0c;这些关键点对旋转、缩放和部分亮度变化具有不变性。计算复杂度: 相对较高&#xff0c;因为SIFT使用了高斯差分核来检测关键点&…

C++ 136类和对象_面像对像_多态_虚析构和纯虚析构

136类和对象_面像对像_多态_虚析构和纯虚析构 学习内容 1.抽象类 2.虚函数 3.纯虚函数 /4.虚析构 和 纯虚析构 总结: 1.虚析构或纯虚析构就是用来解决通过父类指针释放子类对象 2.如果子类中没有堆区数据&#xff0c;可以不写为虚析构或纯虚析构 3.拥有纯虚析构函数的类也属于…

缓存三剑客(穿透,雪崩,击穿)理解

缓存穿透 概念理解 缓存穿透正如其名穿透&#xff0c;说明访问的数据在缓存和数据库里都没用&#xff0c;而且此时还大量的发起了访问&#xff0c;导致数据库崩溃 解决方法 一、第一种解决方法就是保存空值在数据库里面&#xff0c;但是这种情况会很消耗空间 二、第二种办…

利用Python对Excel数据进行条件筛选与排序

目录 一、Python与Excel数据处理的基础知识 1.1 Python中的Excel数据处理库 1.2 pandas库简介 二、使用pandas读取Excel数据 三、Excel数据的条件筛选 3.1 单条件筛选 3.2 多条件筛选 3.3 使用query方法 四、Excel数据的排序 4.1 单列排序 4.2 多列排序 五、案例分…

python模块05-json

3 请求数据提取&#xff1a;json 3.1 json类型数据 json对象&#xff1a;{key:calue} json数组&#xff1a;[1,2,3,4] 3.2 json模块 1&#xff09; json.loads 把Json格式字符串解码转换成Python对象&#xff08;json数组对应列表&#xff0c;json对象对应字典&#xff09…

Jenkins发邮件功能如何配置以实现自动化?

jenkins发邮件的设置指南&#xff1f;Jenkins怎么配置服务器&#xff1f; Jenkins作为一个流行的自动化服务器&#xff0c;其发邮件功能是通知团队成员构建状态的重要手段。AokSend将详细介绍如何配置Jenkins发邮件功能&#xff0c;以实现自动化通知。 Jenkins发邮件&#xf…

『 C++ 』线程库

文章目录 线程库线程的创建与销毁成员函数this_thread 命名空间线程的引用传值 互斥锁互斥锁的基本操作递归锁(可重入锁)定时互斥锁互斥锁管理器与互斥锁抛异常所引发的死锁问题 条件变量条件变量的等待条件变量的唤醒两个线程交替打印奇偶数 线程库 C标准库提供了一套完整的线…

探索 AWS Lightsail 与 EC2:如何选择适合你的云计算服务?

探索 AWS Lightsail 与 EC2&#xff1a;如何选择适合你的云计算服务&#xff1f; 随着云计算的广泛应用&#xff0c;AWS 提供了多种计算服务以满足不同的用户需求。对于初次接触 AWS 的用户来说&#xff0c;可能会在选择 AWS Lightsail 和 EC2 时感到困惑。这两者都提供了虚拟…

webpack打包优化方案

调试工具&#xff1a;安装webpack-bundle-analyzer打包可视化工具&#xff0c;可以看到打包文件大小&#xff0c;从而有针对性的优化。 npm install --save-dev webpack-bundle-analyzer。 方案一&#xff1a;将第三方依赖包使用cdn进行引入减小文件包体积&#xff08;例&…

Git的使用教程及常用语法01

git安装可以到官网上下载并安装&#xff0c;一直点点点就行 安装成功后可以在任意地方右键以终端的形式打开。 打开命令终端&#xff0c;输入git -v 查看git版本 一.配置全局用户名和邮箱 配置全局用户名&#xff1a; git config --global user.name "your username&…

利用TeamCity实现maven项目的CI/CD

1.什么是TeamCity&#xff1f; TeamCity 是一款由 JetBrains 开发的强大的持续集成&#xff08;Continuous Integration&#xff0c;CI&#xff09;和持续部署&#xff08;Continuous Deployment&#xff0c;CD&#xff09;工具。它帮助开发团队自动化构建、测试和部署过程&am…

Scratch的无限可能:突破项目大小与复杂度的界限

Scratch的无限可能&#xff1a;突破项目大小与复杂度的界限 Scratch&#xff0c;这个由麻省理工学院媒体实验室开发的编程平台&#xff0c;以其独特的图形化编程方式&#xff0c;激发了全球数百万孩子的创造力和逻辑思维能力。然而&#xff0c;随着孩子们创意的不断扩展&#…

centos7解决病毒入侵 getty

首先使用top命令查看 找到文件地址 查看是否有自启动服务 关闭、停止、删除 tmp 病毒文件删除 清除标记 [roothost-192-168-0-66 bin]# chattr -ia /tmp/newsvc.sh [roothost-192-168-0-66 bin]# chattr -ia /tmp/redis2 [roothost-192-168-0-66 bin]# chattr -ia /tmp/svc* [r…

C++开发IDE用VisualStudio好还是QtCreator好?

在熟练使用了VisualStudio和QtCreator之后,我依然认为QtCreator作为C++项目开发IDE的便捷性真的相当杰出。 当然了,VisualStudio和QtCreator本身就不是一个量级,VS越做越大,庞大的插件库也使得他能够支持从嵌入式到手机端,从web到脚本,甚至游戏,仿真等等各个领域的开发…