当前位置:网站首页>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();
}边栏推荐
- [Go through 7] Notes from the first section of the fully connected neural network video
- Flink 状态与容错 ( state 和 Fault Tolerance)
- 【技能】长期更新
- [Over 17] Pytorch rewrites keras
- DOM and its applications
- 【过一下15】学习 lstm的一周
- 机器学习(一) —— 机器学习基础
- [Remember 1] June 29, 2022 Brother and brother double pain
- Pandas(五)—— 分类数据、读取数据库
- day7-列表作业(1)
猜你喜欢

Using pip to install third-party libraries in Pycharm fails to install: "Non-zero exit code (2)" solution
![[Over 17] Pytorch rewrites keras](/img/a2/7f0c7eebd119373bf20c44de9f7947.png)
[Over 17] Pytorch rewrites keras

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

【零基础开发NFT智能合约】如何使用工具自动生成NFT智能合约带白名单可Mint无需写代码

SQL(二) —— join窗口函数视图

Flink accumulator Counter 累加器 和 计数器

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

关于基于若依框架的路由跳转

SQL(一) —— 增删改查

【练一下1】糖尿病遗传风险检测挑战赛 【讯飞开放平台】
随机推荐
数据库期末考试,选择、判断、填空题汇总
npm搭建本地服务器,直接运行build后的目录
转正菜鸟前进中的经验(废话)之谈 持续更新中... ...
【过一下15】学习 lstm的一周
[Go through 10] sklearn usage record
Lecture 5 Using pytorch to implement linear regression
对数据排序
[Go through 4] 09-10_Classic network analysis
Mesos learning
[Remember 1] June 29, 2022 Brother and brother double pain
day11-函数作业
day6-列表作业
基于Flink CDC实现实时数据采集(二)-Source接口实现
CAP+BASE
基于Flink CDC实现实时数据采集(四)-Sink接口实现
【练一下1】糖尿病遗传风险检测挑战赛 【讯飞开放平台】
位运算符与逻辑运算符的区别
学习总结day5
通过Flink-Sql将Kafka数据写入HDFS
day8字典作业