AIDL小结
AIDL : Android Interface Define Language(接口定义语言)
Service中跨进程间通信利器。。。。
一般都会有Client端和Server端(Server端提供服务)
实现步骤
1、在Server模块 main文件夹下新建一个aidl文件夹,创建AIDL文件接口,并在Client中相同位置建立相同的包,相同的AIDL文件
2、编译之后会在build-generate-source下生成一个aidl文件夹
3、编写Service
public class IRemoteService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return binder;
}
private IBinder binder=new IMyAidlInterface.Stub(){
@Override
public int plus(int a, int b) throws RemoteException {
return a+b;
}
};
}
4、在AndroidManifest文件中注册该Service,并设置Exported属性为true,否则无法被调用
5、Client中调用
Intent i=new Intent();
i.setComponent(new ComponentName("com.alger.lu_yy.myaidl","com.alger.lu_yy.myaidl.IRemoteService"));//包名及类名
bindService(i, new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
iMyAidlInterface=IMyAidlInterface.Stub.asInterface(service); //将IBinder转成Aidl接口
try {
int count= iMyAidlInterface.plus(10,2);
} catch (RemoteException e) {
e.printStackTrace();
}
} @Override
public void onServiceDisconnected(ComponentName name) {
iMyAidlInterface=null;
}
},BIND_AUTO_CREATE);
注意:发现在Aidl文件中是无法引入相关的类的,能传递的类型有:基本数据类型(short除外),List及Map 这些都是无需Import的,当我们需要传递一些自定义的数据类型时?该如何使用呢?
1、在java文件夹下建立与aidl文件下相同的包,创建相关的类,并实现Parceable接口
public class Person implements Parcelable {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
protected Person(Parcel in) {
this.age=in.readInt();
this.name=in.readString();
}
public static final Creator<Person> CREATOR = new Creator<Person>() {
@Override
public Person createFromParcel(Parcel in) {
return new Person(in);
}
@Override
public Person[] newArray(int size) {
return new Person[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(age);
}
}
2、在aidl文件夹下建立相应的Person.aidl文件,里面写入
parcelable Person;(parceleable必须要小写)
3、并且在Aidl文件中传递自定义的数据类型时必须在类型前面加入in out 关键词
4、同样在Client模块中相同位置建立相同的类及Aidl文件
AIDL小结的更多相关文章
- Android AIDL 小结
1.AIDL (Android Interface Definition Language ) 2.AIDL 适用于 进程间通信,并且与Service端多个线程并发的情况,如果只是单个线程 可以使用 ...
- Android之——AIDL深入
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47071927 在上一篇博文<Android之--AIDL小结>中,我们 ...
- Android之——自己主动挂断电话的实现
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47072451 通过<Android之--AIDL小结>与<And ...
- AIDL使用解析
简书本文地址:点击跳转到简书查看 之前面试的时候被问到这个问题,然而当时只有一个大致的印象,随GG,于是我就重新整理的一下.这里大力推荐<Android开发艺术探索>这本书,写的太好了! ...
- Android app开发知识小结
Android知识小结 这是一个知识的总结,所以没有详解的讲解. 一.分辨率Android中dp长度.sp字体使用.px像素.in英寸.pt英寸1/72.mm毫米 了解dp首先要知道density,d ...
- Android 使用AIDL调用外部服务
好处:多个应用程序之间建立共同的服务机制,通过AIDL在不同应用程序之间达到数据的共享和数据相互操作, 本文包括: 1 .创建AIDL 服务端.2 .创建AIDL 客户端. 3.客户端调用服务端提供的 ...
- Android查缺补漏(IPC篇)-- 款进程通讯之AIDL详解
本文作者:CodingBlock 文章链接:http://www.cnblogs.com/codingblock/p/8436529.html 进程间通讯篇系列文章目录: Android查缺补漏(IP ...
- Android查缺补漏(IPC篇)-- 进程间通讯之AIDL详解
本文作者:CodingBlock 文章链接:http://www.cnblogs.com/codingblock/p/8436529.html 进程间通讯篇系列文章目录: Android查缺补漏(IP ...
- Android AIDL的用法
一.什么是AIDL服务 一般创建的服务并不能被其他的应用程序访问.为了使其他的应用程序也可以访问本应用程序提供的服务,Android系统采用了远程过程调用(Remote Procedure Cal ...
随机推荐
- windows 下 webstorm 使用SVN
1.安装了webstorm之后,用了很久都没有配置SVN 现在想配置svn,结果发现一般的svn程序不好用. 经指导,发现需要安装一个专用于webstorm的SVN 2.在file->setti ...
- VBA编程常用语句
.Option Explicit '强制对模块内所有变量进行声明 Option Private Module '标记模块为私有,仅对同一工程中其它模块有用,在宏对话框中不显示 Option Compa ...
- [IOS8兼容性]IOS8上收不到通知
应用中用到了通知功能,同时有远程通知和本地通知. 测试报告应用在iphone6 plus上,收不到本地通知. 因为所有的第三方闹钟应用采用的都是本地通知方式,所以第一时间随机下载了5款不同的闹钟应用. ...
- Quartz conTrigger 定时器表达式大全
CronTrigger配置格式: 格式: [秒] [分] [小时] [日] [月] [周] [年] 序号 说明 是否必填 允许填写的值 允许的通配符 1 秒 是 0-59 , ...
- phar文件的使用
1.用php命令行 php phar文件 2.生成bat文件,在命令行下使用,以composer.phar为例 ( 1)在php.exe所在目录新建composer.bat文件 (2)把compose ...
- yii2的form表单样式怎么灵活控制呢?
<?php $form = ActiveForm::begin(['id' => 'login-form', 'fieldConfig'=>[ 'template'=> &qu ...
- CentOS MySQL 配置
问题: MySQL安装后root用户无法连接,提示 ERROR 1045 (28000): Access denied for user ’root’@’localhost’ (using passw ...
- 真机测试-Please enter a different string错误解决
错误原因是这个bundle ID已经被占用了,这是想到的是要重置测试证书,那么则需要去修改Bundle identifier,因为测试证书是以Bundle identifier为基准的,修改后运行,重 ...
- 基于Spring Boot/Spring Session/Redis的分布式Session共享解决方案
分布式Web网站一般都会碰到集群session共享问题,之前也做过一些Spring3的项目,当时解决这个问题做过两种方案,一是利用nginx,session交给nginx控制,但是这个需要额外工作较多 ...
- Spring Integration
@ContextConfiguration directs Spring's test runner to locate a configuration file with the same name ...