Binder机制
先看下aidl的实现方式
Service:
定义aidl接口:
把整个aidl的包全部复制到Client相同位置。
创建服务端的service
Client:
在client里面引入服务端aidl后,在client这边bindService()
到这里,就可以Client直接和服务进程通过iMyAidlInterface传数据了。
aidl文件编译时并不会打包进apk,而是生成java文件,aidl更像是一个配置参数,我们定义好方法,aidl生成工具再根据定义的方法生成java文件的模版。打开由编译器根据aidl文件生成好的java文件,可以帮助更好的分析binder 机制。
/*
* This file is auto-generated. DO NOT MODIFY.
*/
package com.example.service;
// 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 com.example.service.IMyAidlInterface
{
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
@Override public void readLine(java.lang.String aString) throws android.os.RemoteException
{
}
@Override
public android.os.IBinder asBinder() {
return null;
}
}
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements com.example.service.IMyAidlInterface
{
private static final java.lang.String DESCRIPTOR = "com.example.service.IMyAidlInterface";
/** Construct the stub at attach it to the interface. */
public Stub()
{
this.attachInterface(this, DESCRIPTOR);
}
/**
* Cast an IBinder object into an com.example.service.IMyAidlInterface interface,
* generating a proxy if needed.
*/
public static com.example.service.IMyAidlInterface asInterface(android.os.IBinder obj)
{
if ((obj==null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof com.example.service.IMyAidlInterface))) {
return ((com.example.service.IMyAidlInterface)iin);
}
return new com.example.service.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_readLine:
{
data.enforceInterface(descriptor);
java.lang.String _arg0;
_arg0 = data.readString();
this.readLine(_arg0);
reply.writeNoException();
return true;
}
default:
{
return super.onTransact(code, data, reply, flags);
}
}
}
private static class Proxy implements com.example.service.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 readLine(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.writeString(aString);
boolean _status = mRemote.transact(Stub.TRANSACTION_readLine, _data, _reply, 0);
if (!_status && getDefaultImpl() != null) {
getDefaultImpl().readLine(aString);
return;
}
_reply.readException();
}
finally {
_reply.recycle();
_data.recycle();
}
}
public static com.example.service.IMyAidlInterface sDefaultImpl;
}
static final int TRANSACTION_readLine = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
public static boolean setDefaultImpl(com.example.service.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.example.service.IMyAidlInterface getDefaultImpl() {
return Stub.Proxy.sDefaultImpl;
}
}
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
public void readLine(java.lang.String aString) throws android.os.RemoteException;
}
先从Client端连接Service成功后的参数看,
这里服务端给返回来的是一个BinderProxy类型,这个就姑且认为是binder的引用吧。。。
调用服务端Binder的readLine()方法之前,要先执行一下类似类型强转的操作
可以看出,Client拿到的实际是一个Stub.Proxy对象
兜兜转转,binder被传给了这个Proxy。而这个Proxy实现了我们定义的借口IMyAidlInterface。下一步,我们该和服务端通信了。调用一下readLine()方法。
来到了Proxy的readLine()方法,到了这里,
可以看出,这些参数传递都是通过序列化来传给底层,就是这个transact()方法。这歌方法第一个参数是一个int类型,就是之前定义aidl文件时写的那些方法,这些方法被一一编号,接下来就是一些binder底层的一些操作,把结果会复制到这个_reply 参数里,这之前还都是在Client进程里,binder底层一系列操作完事以后会回调到服务端Stub的onTransact()方法,而这就已经做完了跨进程。binder是怎么区分这些回调给哪个进程呢,我们注意到在proxy和Stub里都会使用一个DESCRIPTOR标示,而这个表示就是区分进程的。
到这里就算尾声了,onTransact()依然会根据之前编好号的方法编号来决定回调Stub的那个方法。。。