当前位置:网站首页>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();
}边栏推荐
- 【记一下1】2022年6月29日 哥和弟 双重痛苦
- Flink HA安装配置实战
- [Go through 8] Fully Connected Neural Network Video Notes
- 软件设计 实验四 桥接模式实验
- Matplotlib(三)—— 实践
- Calling Matlab configuration in pycharm: No module named 'matlab.engine'; 'matlab' is not a package
- Flink Broadcast 广播变量
- Convert the paper official seal in the form of a photo into an electronic official seal (no need to download ps)
- vscode+pytorch使用经验记录(个人记录+不定时更新)
- 基于Flink CDC实现实时数据采集(一)-接口设计
猜你喜欢
![[Let's pass 14] A day in the study room](/img/fc/ff4161db8ed13a0c8ef75b066b8eab.png)
[Let's pass 14] A day in the study room

【过一下6】机器视觉视频 【过一下2被挤掉了】

【过一下14】自习室的一天

软件设计 实验四 桥接模式实验

flink项目开发-flink的scala shell命令行交互模式开发
![[Study Notes Dish Dog Learning C] Classic Written Exam Questions of Dynamic Memory Management](/img/0b/f7d9205c616f7785519cf94853d37d.png)
[Study Notes Dish Dog Learning C] Classic Written Exam Questions of Dynamic Memory Management

IDEA 配置连接数据库报错 Server returns invalid timezone. Need to set ‘serverTimezone‘ property.

第二讲 Linear Model 线性模型

【练一下1】糖尿病遗传风险检测挑战赛 【讯飞开放平台】

SQL(一) —— 增删改查
随机推荐
2022年中总结关键词:裁员、年终奖、晋升、涨薪、疫情
[Redis] Resid的删除策略
range函数作用
如何编写一个优雅的Shell脚本(二)
day6-列表作业
Do you use tomatoes to supervise your peers?Add my study room, come on together
【过一下3】卷积&图像噪音&边缘&纹理
学习总结week2_4
第5讲 使用pytorch实现线性回归
HQL语句执行过程
基于Flink CDC实现实时数据采集(四)-Sink接口实现
【Untitled】
vscode+pytorch use experience record (personal record + irregular update)
鼠标放上去变成销售效果
The fourth back propagation back propagation
BFC详解(Block Formmating Context)
有用番茄来监督自己的同道中人吗?加一下我的自习室,一起加油
解决端口占用问题
学习总结week3_1函数
Oracle压缩表修改字段的处理方法