关键词:
文章目录
一、创建 AIDL 文件
1、创建 AIDL 目录
在 Android Studio 工程中 , 创建 aidl 文件 ;
右键点击 main 目录 , 选择 " New / Directory " ,
选择创建 " aidl " 目录 ,
创建好的 aidl 目录如下 :
2、创建 AIDL 文件
右键点击 " aidl " 目录 , 选择 " New / AIDL / AIDL File " 文件 ;
弹出对话框 , 输入 AIDL 接口名称 , 输入完毕后 , 选择 " Finish " 选项 ;
3、创建 Parcelable 类
创建 Parcelable 类 :
package kim.hsl.aidl_demo;
import android.os.Parcel;
import android.os.Parcelable;
public class Student implements Parcelable
private String name;
public Student(String name)
this.name = name;
public String getName()
return name;
public void setName(String name)
this.name = name;
protected Student(Parcel in)
name = in.readString();
public static final Creator<Student> CREATOR = new Creator<Student>()
@Override
public Student createFromParcel(Parcel in)
return new Student(in);
@Override
public Student[] newArray(int size)
return new Student[size];
;
@Override
public int describeContents()
return 0;
@Override
public void writeToParcel(Parcel dest, int flags)
dest.writeString(name);
public void readFromParcel(Parcel desc)
name = desc.readString();
@Override
public String toString()
return "name=" + name;
4、AIDL 目录下声明 Parcelable 类
在 aidl 目录下声明 Parcelable 类 : 在 aidl 目录下创建 Student.aidl 文件 , 然后声明如下内容 ;
package kim.hsl.aidl_demo;
parcelable Student;
5、AIDL 中使用 Parcelable 类
在创建的 AIDL 接口中使用 Student 类 :
① 首先要导入 Student 类 , import kim.hsl.aidl_demo.Student;
② 参数的输入输出 , in 写入, out 输出, inout 写入和输出 ;
// IMyAidlInterface.aidl
package kim.hsl.aidl_demo;
import kim.hsl.aidl_demo.Student;
// Declare any non-default types here with import statements
interface IMyAidlInterface
/**
* 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);
/**
* in 写入, out 输出, inout 写入和输出
*/
void addStudent(inout Student student);
/**
* 获取 Student 集合
*/
List<Student> getStudents();
二、编译工程生成 AIDL 文件对应的 Java 源文件
1、编译工程
点击 " 菜单栏 / Build / Make Project " 选项 , 即可编译当前的工程 , 进而生成 AIDL 接口对应的 Java 源文件 ;
编译后 , 在 " AIDL_Demo\\app\\build\\generated\\aidl_source_output_dir\\debug\\out\\kim\\hsl\\aidl_demo " 目录 , 生成了 AIDL 文件对应的源码 :
2、生成的 AIDL 对应 Java 源文件
下面的源码是编译生成的 Java 源文件 :
/*
* This file is auto-generated. DO NOT MODIFY.
*/
package kim.hsl.aidl_demo;
// Declare any non-default types here with import statements
public interface IMyAidlInterface extends android.os.IInterface
/** Default implementation for IMyAidlInterface. */
public static class Default implements kim.hsl.aidl_demo.IMyAidlInterface
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
@Override public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, java.lang.String aString) throws android.os.RemoteException
/**
* in 写入, out 输出, inout 写入和输出
*/
@Override public void addStudent(kim.hsl.aidl_demo.Student student) throws android.os.RemoteException
/**
* 获取 Student 集合
*/
@Override public java.util.List<kim.hsl.aidl_demo.Student> getStudents() throws android.os.RemoteException
return null;
@Override
public android.os.IBinder asBinder()
return null;
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements kim.hsl.aidl_demo.IMyAidlInterface
private static final java.lang.String DESCRIPTOR = "kim.hsl.aidl_demo.IMyAidlInterface";
/** Construct the stub at attach it to the interface. */
public Stub()
this.attachInterface(this, DESCRIPTOR);
/**
* Cast an IBinder object into an kim.hsl.aidl_demo.IMyAidlInterface interface,
* generating a proxy if needed.
*/
public static kim.hsl.aidl_demo.IMyAidlInterface asInterface(android.os.IBinder obj)
if ((obj==null))
return null;
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof kim.hsl.aidl_demo.IMyAidlInterface)))
return ((kim.hsl.aidl_demo.IMyAidlInterface)iin);
return new kim.hsl.aidl_demo.IMyAidlInterface.Stub.Proxy(obj);
@Override public android.os.IBinder asBinder()
return this;
@Override public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException
java.lang.String descriptor = DESCRIPTOR;
switch (code)
case INTERFACE_TRANSACTION:
reply.writeString(descriptor);
return true;
case TRANSACTION_basicTypes:
data.enforceInterface(descriptor);
int _arg0;
_arg0 = data.readInt();
long _arg1;
_arg1 = data.readLong();
boolean _arg2;
_arg2 = (0!=data.readInt());
float _arg3;
_arg3 = data.readFloat();
double _arg4;
_arg4 = data.readDouble();
java.lang.String _arg5;
_arg5 = data.readString();
this.basicTypes(_arg0, _arg1, _arg2, _arg3, _arg4, _arg5);
reply.writeNoException();
return true;
case TRANSACTION_addStudent:
data.enforceInterface(descriptor);
kim.hsl.aidl_demo.Student _arg0;
if ((0!=data.readInt()))
_arg0 = kim.hsl.aidl_demo.Student.CREATOR.createFromParcel(data);
else
_arg0 = null;
this.addStudent(_arg0);
reply.writeNoException();
if ((_arg0!=null))
reply.writeInt(1);
_arg0.writeToParcel(reply, android.os.Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
else
reply.writeInt(0);
return true;
case TRANSACTION_getStudents:
data.enforceInterface(descriptor);
java.util.List<kim.hsl.aidl_demo.Student> _result = this.getStudents();
reply.writeNoException();
reply.writeTypedList(_result);
return true;
default:
return super.onTransact(code, data, reply, flags);
private static class Proxy implements kim.hsl.aidl_demo.IMyAidlInterface
private android.os.IBinder mRemote;
Proxy(android.os.IBinder remote)
mRemote = remote;
@Override public android.os.IBinder asBinder()
return mRemote;
public java.lang.String getInterfaceDescriptor()
return DESCRIPTOR;
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
@Override public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, java.lang.String aString) throws android.os.RemoteException
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
try
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeInt(anInt);
_data.writeLong(aLong);
_data.writeInt(((aBoolean)?(1):(0)));
_data.writeFloat(aFloat);
_data.writeDouble(aDouble);
_data.writeString(aString);
boolean _status = mRemote.transact(Stub.TRANSACTION_basicTypes, _data, _reply, 0);
if (!_status && getDefaultImpl() != null)
getDefaultImpl().basicTypes(anInt, aLong, aBoolean, aFloat, aDouble, aString);
return;
_reply.readException();
finally
_reply.recycle();
_data.recycle();
/**
* in 写入, out 输出, inout 写入和输出
*/
@Override public void addStudent(kim.hsl.aidl_demo.Student student) throws android.os.RemoteException
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
try
_data.writeInterfaceToken(DESCRIPTOR);
if ((student!=null))
_data.writeInt(1);
student.writeToParcel(_data, 0);
else
_data.writeInt(0);
boolean _status = mRemote.transact(Stub.TRANSACTION_addStudent, _data, _reply, 0);
if (!_status && getDefaultImpl() != null)
getDefaultImpl().addStudent(student);
return;
_reply.readException();
if ((0!=_reply.readInt()))
student.readFromParcel(_reply);
finally
_reply.recycle();
_data.recycle();
/**
* 获取 Student 集合
*/
@Override public java.util.List<kim.hsl.aidl_demo.Student> getStudents() throws android.os.RemoteException
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
java.util.List<kim.hsl.aidl_demo.Student> _result;
try
_data.writeInterfaceToken(DESCRIPTOR);
boolean _status = mRemote.transact(Stub.TRANSACTION_getStudents, _data, _reply, 0);
if (!_status && getDefaultImpl() != null)
return getDefaultImpl().getStudents();
_reply.readException();
_result = _reply.createTypedArrayList(kim.hsl.aidl_demo.Student.CREATOR);
finally
_reply.recycle();
_data.recycle();
return _result;
public static kim.hsl.aidl_demo.IMyAidlInterface sDefaultImpl;
static final int TRANSACTION_basicTypes = (android.os.IBinder查看详情
binder机制aidl分析(aidl通信完整流程梳理)(代码片段)
...Stub.Proxy代理类5、IMyAidlInterface.Stub.Proxy代理类方法执行6、Binder.transact方法执行7、IMyAidlInterface.Stub.onTransac 查看详情
binder的工作机制浅析
在Android开发中,Binder主要用于Service中,包括AIDL和Messenger,其中Messenger的底层实现就是AIDL,所以我们这里通过AIDL来分析一下Binder的工作机制。一、在AndroidStudio中建立AIDL首先,我们需要建立一个AIDL1.在建立了对应的实现Parcelable... 查看详情
binder机制在aidl中的实现分析
本篇主要通过结合已经阅读的Binder机制相关资料(《Android开发艺术探索》和http://weishu.me/2016/01/12/binder-index-for-newer/),通过AIDL来进行Binder机制的初步理解感谢两位作者:任玉刚和WeiShu一一个AIDLDemo的组成部分二通信机制的分析1bindS... 查看详情
binder机制在aidl中的实现分析(代码片段)
本篇主要通过结合已经阅读的Binder机制相关资料(《Android开发艺术探索》和http://weishu.me/2016/01/12/binder-index-for-newer/),通过AIDL来进行Binder机制的初步理解感谢两位作者:任玉刚和WeiShu一一个AIDLDemo的组成部分二通信机制的... 查看详情
binder机制在aidl中的实现分析(代码片段)
本篇主要通过结合已经阅读的Binder机制相关资料(《Android开发艺术探索》和http://weishu.me/2016/01/12/binder-index-for-newer/),通过AIDL来进行Binder机制的初步理解感谢两位作者:任玉刚和WeiShu一一个AIDLDemo的组成部分二通信机制的... 查看详情
源码分析——从aidl的使用开始理解binder进程间通信的流程
源码分析——从AIDL的使用开始理解Binder进程间通信的流程Binder通信是Android系统架构的基础。本文尝试从AIDL的使用开始理解系统的Binder通信。0x00一个AIDL的例子首先我们创建一个项目,写一个RemoteService.java,并定义个AI... 查看详情
android基础——binder连接池连接多个aidl文件的处理(代码片段)
Binder连接池连接多个AIDL文件的处理 事先说明:本人也是个初学者,所以本文是从初学者的角度入手,如果有不妥的地方请留言教导我,谢谢。如果对AIDL的使用和Binder机制不懂的,可以参照我之前的文章,Android基础——... 查看详情
androidframework实战开发-binder专题讲解之aidl文件的详细分析(代码片段)
...l,service。确实这个aidl和service的方式是应用开发中对binder接触层面应该属于最为接近的一层。因为其他的接口方式跨进程通信,比如startActivtiy,ContentProvider,broadc 查看详情
bindservice之aidl使用和调用流程分析
...tub类,以及私有Proxy,这是客户端client和服务端Service通过binder驱动通讯用。2、提供外部绑定的service以及在XML上声明权限:ServerService.java:其中onBind返回的对象IBinder给客户端client绑定服务成功回调函数onServiceConnected。XML声明permissi... 查看详情
androidframework实战开发-binder专题讲解之aidl文件的详细分析(代码片段)
csdn在线学习课程,课程咨询答疑和新课信息:QQ交流群:422901085进行课程讨论android跨进程通信实战视频课程(加群获取优惠)大家平时做应用开发时候也经常会遇到有跨进程通信的需求,这里大部分通信... 查看详情
不得不说的androidbinder机制与aidl(代码片段)
...进程间通信,想必大家都会不约而同的想起Android中的Binder机制。而提起Binder,想必也有不少同学会想起初学Android时被Binder和AIDL支配的恐惧感。但是作为一个Android开发者,Binder是我们必须掌握的知识。因为它是构架... 查看详情
androidbinder原理
参考技术A以前看源码经常会看到Binder的东西,比如AMS,ActivityThread等,之前虽然对Binder有所了解,但也是模模糊糊的,这次终于下定决心好好的弄一弄它的原理,揭开它头上的那块面纱。首先,Binder主要是Android跨进程通信的一种... 查看详情
基于binder的跨进程通讯之使用aidl实现demo(代码片段)
写在前面上一篇我们介绍了binder机制的基本知识,如果还不太了解binder机制,可进行点击查看:让你一看就明白的Binder机制binder和AIDL的关系从应用层的角度来说,Binder类是android中的一个类,它实现了IBinder接... 查看详情
androidbinder,aidl跨进程通讯详解与实现,看一遍就懂(代码片段)
1.说到AIDL,就会联想到Binder机制, Binder是一种进程间通信机制 整个app属于客户端,系统是服务端,他们之间的通讯就是通过IPC交互,中间服务就是serviceSystem ... 查看详情
使用aidl
...p;首先绑定服务端的Service,绑定成功后,将服务端返回的Binder对象转成AIDL接口所属的类型,接着就可以调用AIDL中的方法了。需要注意的是,AIDL的包结构在服务端和客户端要保持一致,否则运行会出错,这是因为客户端需要反序... 查看详情
binder连接池
Binder连接池首先回顾一下AIDL的大致流程:首先创建一个Service和一个AIDL接口,接着创建一个类继承自AIDL接口中的Stub类并实现Stub中的抽象方法,在Service的onBind方法中返回这个类的对象,然后客户端就可以绑定Service... 查看详情
从aild与bindservice谈binder进程间通信原理(上)
从AILD与bindService谈Binder进程间通信原理(上)前言Android进程间通信可以分为三种方式:Binder机制,文件读写,Socket机制。这篇文章主要就是来谈谈Binder机制实现进程间通信的原理,主要分析AIDL进程间通信和bindS... 查看详情