Android的进程间通信是有区别与Linux的进程间通信方式(Socket,管道等等),Android采取Binder内核驱动的方式,实现进程间的通信。因为Android系统是基于Linux系统改写的,Linux的内核是有权限访问所有进程的通信的,那么Android系统就在Linux的内核扩展了自己的代码,Android称之为Binder驱动,那么Android进程间的通信都是依赖于Binder驱动来实现的,Binder驱动是最底层的东西,我们可以尝试去看看Binder驱动的源代码。
应用
在详细的了解Android进程间通信原理之前,我们要先了解在应用层是如何实现进程间通信的,在应用层我们通过AIDL来实现进程间的通信,可以通过AIDL实现不同进程间传送数据(基本类型和对象),编写AIDL是很简单的,通过Android studio开发工具,就可以生成AIDL和对应的Java类,其实AIDL文本只是Android为了开发者能够更简单实现进程间通信发明的,只序开发者编写AIDL文件即可,而不用花时间去实现Java类,但是如果我们想要了解Android的进程间通信,还需要自己去实现Java类。
编写AIDL文件
非常简单的AIDL文件,只需这样,点击build->Make Project(如果无法生成,可以使用aidl命令),就可以生成对应的Java类。
对应Java类:
/*
* This file is auto-generated. DO NOT MODIFY.
* Original file: /home/zouzhiyi/workspace/UltimateRecyclerView-master/IPCServive/app/src/main
* /aidl/com/ipc/scott/ipcservive/DownloadManager.aidl
*/
package com.ipc.scott.ipcservive;
public interface DownloadManager extends android.os.IInterface
{
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements
DownloadManager
{
private static final String DESCRIPTOR =
"com.ipc.scott.ipcservive.DownloadManager";
/** Construct the stub at attach it to the interface. */
public Stub()
{
this.attachInterface(this, DESCRIPTOR);
}
/**
* Cast an IBinder object into an com.ipc.scott.ipcservive.DownloadManager interface,
* generating a proxy if needed.
*/
public static DownloadManager asInterface(android.os.IBinder obj)
{
if ((obj == null))
{
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin != null) && (iin instanceof DownloadManager)))
{
return ((DownloadManager) iin);
}
return new 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
{
switch (code)
{
case INTERFACE_TRANSACTION:
{
reply.writeString(DESCRIPTOR);
return true;
}
case TRANSACTION_registDownloadCallback:
{
data.enforceInterface(DESCRIPTOR);
DownloadCallback _arg0;
_arg0 = DownloadCallback.Stub.asInterface(
data.readStrongBinder());
this.registDownloadCallback(_arg0);
reply.writeNoException();
return true;
}
case TRANSACTION_unRegistDownloadCallback:
{
data.enforceInterface(DESCRIPTOR);
DownloadCallback _arg0;
_arg0 = DownloadCallback.Stub.asInterface(
data.readStrongBinder());
this.unRegistDownloadCallback(_arg0);
reply.writeNoException();
return true;
}
}
return super.onTransact(code, data, reply, flags);
}
private static class Proxy implements DownloadManager
{
private android.os.IBinder mRemote;
Proxy(android.os.IBinder remote)
{
mRemote = remote;
}
@Override
public android.os.IBinder asBinder()
{
return mRemote;
}
public String getInterfaceDescriptor()
{
return DESCRIPTOR;
}
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
@Override
public void registDownloadCallback(DownloadCallback callback)
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.writeStrongBinder(
(((callback != null)) ? (callback.asBinder()) : (null)));
mRemote.transact(Stub.TRANSACTION_registDownloadCallback, _data, _reply, 0);
_reply.readException();
} finally
{
_reply.recycle();
_data.recycle();
}
}
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
@Override
public void unRegistDownloadCallback(DownloadCallback callback)
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.writeStrongBinder(
(((callback != null)) ? (callback.asBinder()) : (null)));
mRemote.transact(Stub.TRANSACTION_unRegistDownloadCallback, _data, _reply, 0);
_reply.readException();
} finally
{
_reply.recycle();
_data.recycle();
}
}
}
static final int TRANSACTION_registDownloadCallback =
(android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
static final int TRANSACTION_unRegistDownloadCallback =
(android.os.IBinder.FIRST_CALL_TRANSACTION + 1);
}
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
public void registDownloadCallback(DownloadCallback callback)
throws android.os.RemoteException;
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
public void unRegistDownloadCallback(DownloadCallback callback)
throws android.os.RemoteException;
}
AIDL文件没什么好研究的,按着规则来就可以实现不同进程间的通信,将对应的Java类打包交给客户端使用即可。
在这里好好的研究下自动生成的Java类,这个DownloadManager的类图,涉及到几个主要的类,
- android.os.IInterface
- android.os.Binder
- android.os.IBinder
android.os.Parcel
这4个类都是android.os下的类,所有是系统级别的类。