一、简介
AIDL(Android Interface definition language/android 接口描述语言)是一个IDL语言,它可以生成一段代码,可以使在一个android设备上运行的两个进程使用内部通信进程进行交互。如果你需要在一个进程中(例如:在一个Activity中)访问另一个进程中(例如:一个Service)某个对象的方法,可以使用AIDL来生成这样的代码来伪装传递各种参数。
使用AIDL,Service需要以aidl文件的方式提供服务接口,AIDL工具将生成一个相应的java接口,并且在生成的服务接口中包含一个功能调用的stub服务桩类。Service的实现类需要去继承这个stub服务桩类。Service的onBind方法会返回实现类的对象,之后你就可以使用它了
注:新建AIDL文件后,会生成com.testaidl.IMyAidlInterface.aidl文件,并会生成一个默认的方法。此时需要重写自定义的方法。
选择build->make project会在build目录生成IMyAidlInterface.java文件
三、分析
生成AIDL文件
/*
* This file is auto-generated. DO NOT MODIFY.
*/
package com.testaidl;
// Declare any non-default types here with import statements
public interface IMyAidlInterface extends android.os.IInterface
{
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements com.testaidl.IMyAidlInterface
{
private static final java.lang.String DESCRIPTOR = "com.testaidl.IMyAidlInterface";
/** Construct the stub at attach it to the interface. */
public Stub()
{
this.attachInterface(this, DESCRIPTOR);
}
/**
* Cast an IBinder object into an com.testaidl.IMyAidlInterface interface,
* generating a proxy if needed.
*/
//用于将服务端的Binder对象转换成客户端所需的AIDL接口类型的对象
//如果客户端和服务端位于同一个进程,那么此方法返回的就是服务端的Stub对象本身,否则返回的是系统封装后的Stub.proxy对象
public static com.testaidl.IMyAidlInterface asInterface(android.os.IBinder obj)
{
if ((obj==null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof com.testaidl.IMyAidlInterface))) {
return ((com.testaidl.IMyAidlInterface)iin);
}
return new com.testaidl.IMyAidlInterface.Stub.Proxy(obj);
}
//用于返回当前Binder对象
@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_setName:
{
data.enforceInterface(descriptor);
java.lang.String _arg0;
_arg0 = data.readString();
boolean _result = this.setName(_arg0);
reply.writeNoException();
reply.writeInt(((_result)?(1):(0)));
return true;
}
case TRANSACTION_getName:
{
data.enforceInterface(descriptor);
java.lang.String _result = this.getName();
reply.writeNoException();
reply.writeString(_result);
return true;
}
case TRANSACTION_getAge:
{
data.enforceInterface(descriptor);
int _result = this.getAge();
reply.writeNoException();
reply.writeInt(_result);
return true;
}
case TRANSACTION_setAge:
{
data.enforceInterface(descriptor);
int _arg0;
_arg0 = data.readInt();
this.setAge(_arg0);
reply.writeNoException();
return true;
}
default:
{
return super.onTransact(code, data, reply, flags);
}
}
}
private static class Proxy implements com.testaidl.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.
*/// void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
// double aDouble, String aString);
@Override public boolean setName(java.lang.String name) throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
boolean _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeString(name);
boolean _status = mRemote.transact(Stub.TRANSACTION_setName, _data, _reply, 0);
if (!_status && getDefaultImpl() != null) {
return getDefaultImpl().setName(name);
}
_reply.readException();
_result = (0!=_reply.readInt());
}
finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
@Override public java.lang.String getName() throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
java.lang.String _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
boolean _status = mRemote.transact(Stub.TRANSACTION_getName, _data, _reply, 0);
if (!_status && getDefaultImpl() != null) {
return getDefaultImpl().getName();
}
_reply.readException();
_result = _reply.readString();
}
finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
@Override public int getAge() throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
int _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
boolean _status = mRemote.transact(Stub.TRANSACTION_getAge, _data, _reply, 0);
if (!_status && getDefaultImpl() != null) {
return getDefaultImpl().getAge();
}
_reply.readException();
_result = _reply.readInt();
}
finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
@Override public void setAge(int age) 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(age);
boolean _status = mRemote.transact(Stub.TRANSACTION_setAge, _data, _reply, 0);
if (!_status && getDefaultImpl() != null) {
getDefaultImpl().setAge(age);
return;
}
_reply.readException();
}
finally {
_reply.recycle();
_data.recycle();
}
}
public static com.testaidl.IMyAidlInterface sDefaultImpl;
}
static final int TRANSACTION_setName = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
static final int TRANSACTION_getName = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);
static final int TRANSACTION_getAge = (android.os.IBinder.FIRST_CALL_TRANSACTION + 2);
static final int TRANSACTION_setAge = (android.os.IBinder.FIRST_CALL_TRANSACTION + 3);
public static boolean setDefaultImpl(com.testaidl.IMyAidlInterface impl) {
// Only one user of this interface can use this function
// at a time. This is a heuristic to detect if two different
// users in the same process use this function.
if (Stub.Proxy.sDefaultImpl != null) {
throw new IllegalStateException("setDefaultImpl() called twice");
}
if (impl != null) {
Stub.Proxy.sDefaultImpl = impl;
return true;
}
return false;
}
public static com.testaidl.IMyAidlInterface getDefaultImpl() {
return Stub.Proxy.sDefaultImpl;
}
}
/**
* 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);
public boolean setName(java.lang.String name) throws android.os.RemoteException;
public java.lang.String getName() throws android.os.RemoteException;
public int getAge() throws android.os.RemoteException;
public void setAge(int age) throws android.os.RemoteException;
}
示例:
MainActivity.java
package com.testaidl;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
IMyAidlInterface mService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intentService = new Intent(this,testServer.class);
bindService(intentService,mConnection,this.BIND_AUTO_CREATE);
}
ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mService = IMyAidlInterface.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
public void onClick(View view) {
switch (view.getId()){
case R.id.getNameBt:
try {
Toast.makeText(MainActivity.this,mService.getName()+"",Toast.LENGTH_LONG).show();
} catch (RemoteException e) {
e.printStackTrace();
}
break;
case R.id.getAgeBt:
try {
Toast.makeText(MainActivity.this,mService.getAge()+"",Toast.LENGTH_LONG).show();
} catch (RemoteException e) {
e.printStackTrace();
}
break;
case R.id.setNameBt:
try {
Toast.makeText(MainActivity.this,mService.setName("hell")+"",Toast.LENGTH_LONG).show();
} catch (RemoteException e) {
e.printStackTrace();
}
break;
case R.id.setAgeBt:
try {
mService.setAge(100);
} catch (RemoteException e) {
e.printStackTrace();
}
break;
}
}
}
testServer.java
package com.testaidl;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import androidx.annotation.Nullable;
public class testServer extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return mBind;
}
IMyAidlInterface.Stub mBind = new IMyAidlInterface.Stub() {
String mName;
int mAge;
@Override
public boolean setName(String name) throws RemoteException {
if(name == null)
return false;
else{
mName = name;
return true;
}
}
@Override
public String getName() throws RemoteException {
return mName;
}
@Override
public int getAge() throws RemoteException {
return mAge;
}
@Override
public void setAge(int age) throws RemoteException {
mAge = age;
}
};
}
四、方法分析
1、transact()方法
该方法实际上是通过底层的Binder驱动调用到C/C++层的JavaBBinder对象的tansact方法,而该方法通过JNI来调用JAVA 层上Binder对象的execTransact()方法,
并进而调用开发者自己可以覆盖的onTransact()方法从而达到了通信的目标
2、attachInterface方法
//frameworks/base/core/java/android/os/Binder.java
//这个函数属于Binder类
public void attachInterface(@Nullable IInterface owner, @Nullable String descriptor) {
mOwner = owner;
mDescriptor = descriptor;
}
3、queryLocalInterface方法
//frameworks/base/core/java/android/os/Binder.java
//这个函数属于Binder类
public @Nullable IInterface queryLocalInterface(@NonNull String descriptor) {
if (mDescriptor != null && mDescriptor.equals(descriptor)) {
return mOwner;
}
return null;
}
4、stub
什么是stub
存根类是一个类,它实现了一个接口,但是实现后的每个方法都是空的。
如果一个接口有很多方法,如果要实现这个接口,就要实现所有的方法。
但是一个类从业务来说,可能只需要其中一两个方法。如果直接去实现这个接口,
除了实现所需的方法,还要实现其他所有的无关方法。
而如果通过继承存根类就实现接口,就免去了这种麻烦。
交互过程client<-->proxy<-->stub<-->service
stub和proxy是为了方便client/service交互而生成出来的代码,