当前位置:网站首页>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();
}边栏推荐
- flink on yarn 集群模式启动报错及解决方案汇总
- Matplotlib(二)—— 子图
- [Go through 3] Convolution & Image Noise & Edge & Texture
- 拿出接口数组对象中的所有name值,取出同一个值
- 学习总结week3_2函数进阶
- 【零基础开发NFT智能合约】如何使用工具自动生成NFT智能合约带白名单可Mint无需写代码
- [Go through 10] sklearn usage record
- 【Reading】Long-term update
- The difference between the operators and logical operators
- Spark ML学习相关资料整理
猜你喜欢

Flink HA配置

Database experiment five backup and recovery
![[Go through 8] Fully Connected Neural Network Video Notes](/img/0a/8b2510b5536621f402982feb0a01ef.png)
[Go through 8] Fully Connected Neural Network Video Notes

Using pip to install third-party libraries in Pycharm fails to install: "Non-zero exit code (2)" solution

Lecture 4 Backpropagation Essays

Lecture 2 Linear Model Linear Model

【NFT网站】教你制作开发NFT预售网站官网Mint作品

Pycharm中使用pip安装第三方库安装失败:“Non-zero exit code (2)“的解决方法

CAP+BASE

Day1:用原生JS把你的设备变成一台架子鼓!
随机推荐
Redux
Lecture 5 Using pytorch to implement linear regression
【过一下12】整整一星期没记录
Flink EventTime和Watermarks案例分析
[Go through 7] Notes from the first section of the fully connected neural network video
Do you use tomatoes to supervise your peers?Add my study room, come on together
【MySQL】数据库多表链接的查询方式
【过一下6】机器视觉视频 【过一下2被挤掉了】
【技能】长期更新
The software design experiment four bridge model experiment
【过一下14】自习室的一天
【过一下8】全连接神经网络 视频 笔记
day6-列表作业
flink项目开发-flink的scala shell命令行交互模式开发
基于Flink CDC实现实时数据采集(二)-Source接口实现
位运算符与逻辑运算符的区别
flink部署操作-flink standalone集群安装部署
The difference between the operators and logical operators
IDEA 配置连接数据库报错 Server returns invalid timezone. Need to set ‘serverTimezone‘ property.
如何编写一个优雅的Shell脚本(三)