当前位置:网站首页>AIDL详解
AIDL详解
2022-08-05 05:15:00 【suiyue010211】
目录
AIDL是什么?
Android 通信接口定义语言
AIDL用在哪?
两个app进程间通信
如果现在想要进行进程间通信,应该如何操作?
分为客户端和服务端
服务端应该做什么?
1. 定义.aidl文件,定义具体业务的抽象方法
interface ServiceAidlInterface {
void setname(String name);
void setmoney(int money);
String getinfo();
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
double aDouble, String aString);
}
2. 将项目rebuild,系统会自动生成和.aidl文件同名的java文件
3. 创建新的类,让类继承 接口.Stub
public class SericeImpl extends ServiceAidlInterface.Stub{
private String name;
private int money;
@Override
public void setname(String name) throws RemoteException {
this.name=name;
}
@Override
public void setmoney(int money) throws RemoteException {
this.money=money;
}
@Override
public String getinfo() throws RemoteException {
Intent intent=new Intent();
intent.setClass(MyService.this,MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("name",name);
intent.putExtra("money",money);
startActivity(intent);
return "你的名字是"+name+"花了"+money+"块";
}
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
}
}
4. 向客户端公开接口,依托于服务
public class MyService extends Service {
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return new SericeImpl();
}
客户端做什么?
1. 将服务器的.aidl连包一起放入客户端,将项目rebuild
2. 在绑定服务的过程中,发现无法对服务器公开的接口进行连接(API 30)
<queries>
<package android:name="com.wzk.serviceapptoo"/>
</queries>
3. 在Activity中绑定服务
Intent intent=new Intent();
intent.setComponent(new ComponentName("com.wzk.serviceapptoo","com.wzk.serviceapptoo.MyService"));
4.绑定服务后,跟服务器开始进行对话
bindService(intent,conn,BIND_AUTO_CREATE);
try {
serviceAidlInterface.setname("小小");
serviceAidlInterface.setmoney(5000);
String getinfo = serviceAidlInterface.getinfo();
Log.i("服务", "onClick: "+getinfo);
} catch (RemoteException e) {
e.printStackTrace();
}
边栏推荐
猜你喜欢
软件设计 实验四 桥接模式实验
el-table,el-table-column,selection,获取多选选中的数据
[Go through 3] Convolution & Image Noise & Edge & Texture
DOM及其应用
Flutter 3.0升级内容,该如何与小程序结合
将照片形式的纸质公章转化为电子公章(不需要下载ps)
Flink Table API 和 SQL之概述
Calling Matlab configuration in pycharm: No module named 'matlab.engine'; 'matlab' is not a package
第四讲 反向传播随笔
【MySQL】数据库多表链接的查询方式
随机推荐
LeetCode: 1403. Minimum subsequence in non-increasing order [greedy]
有用番茄来监督自己的同道中人吗?加一下我的自习室,一起加油
Flink 状态与容错 ( state 和 Fault Tolerance)
[Skill] Long-term update
redis 缓存清除策略
基于Flink CDC实现实时数据采集(三)-Function接口实现
学习总结week3_3迭代器_模块
拿出接口数组对象中的所有name值,取出同一个值
浅谈Servlet生命周期
Lecture 4 Backpropagation Essays
The fourth back propagation back propagation
Matplotlib(三)—— 实践
Spark ML学习相关资料整理
MySql之索引
DOM及其应用
【过一下8】全连接神经网络 视频 笔记
day10-字符串作业
[Go through 9] Convolution
redis persistence
npm搭建本地服务器,直接运行build后的目录