Android aidl之间的通信在我看来其实就是Android系统内部的一套通信系统 我们在用的时候通过创建aidl文件来实现Android进程之间的通信
1.我们所定义的通信bean类要实现 Parcelable 这个接口 其实我认为就是现实了可以在底层转化数据的一种方法
2.自己定义的接口方法
package com.text.aidl.bean;
import com.text.aidl.bean.Student;
import com.text.aidl.bean.IClassInterface;
interface IStudentManager {
void setClassInterface (IClassInterface call);
List<Student> getAllStudent ();
Student getStudent (in int id);
void addStudent (in Student stu);
}
和
package com.text.aidl.bean;
import com.text.aidl.bean.Student;
interface IClassInterface {
void callStudent(in Student stu);
void callClassId (in int id);
}
这个接口aidl都会在as中自动生成
package com.text.aidl.bean;
public interface IClassInterface extends android.os.IInterface {
/**
* Local-side IPC implementation stub class.
*/
public static abstract class Stub extends android.os.Binder implements com.text.aidl.bean.IClassInterface {
private static final java.lang.String DESCRIPTOR = "com.text.aidl.bean.IClassInterface";
/**
* Construct the stub at attach it to the interface.
*/
public Stub() {
this.attachInterface(this, DESCRIPTOR);
}
/**
* Cast an IBinder object into an com.text.aidl.bean.IClassInterface interface,
* generating a proxy if needed.
*/
public static com.text.aidl.bean.IClassInterface asInterface(android.os.IBinder obj) {
if ((obj == null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin != null) && (iin instanceof com.text.aidl.bean.IClassInterface))) {
return ((com.text.aidl.bean.IClassInterface) iin);
}
return new com.text.aidl.bean.IClassInterface.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 {
switch (code) {
case INTERFACE_TRANSACTION: {
reply.writeString(DESCRIPTOR);
return true;
}
case TRANSACTION_callStudent: {
data.enforceInterface(DESCRIPTOR);
com.text.aidl.bean.Student _arg0;
if ((0 != data.readInt())) {
_arg0 = com.text.aidl.bean.Student.CREATOR.createFromParcel(data);
} else {
_arg0 = null;
}
this.callStudent(_arg0);
reply.writeNoException();
return true;
}
case TRANSACTION_callClassId: {
data.enforceInterface(DESCRIPTOR);
int _arg0;
_arg0 = data.readInt();
this.callClassId(_arg0);
reply.writeNoException();
return true;
}
}
return super.onTransact(code, data, reply, flags);
}
private static class Proxy implements com.text.aidl.bean.IClassInterface {
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;
}
@Override
public void callStudent(com.text.aidl.bean.Student stu) 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 ((stu != null)) {
_data.writeInt(1);
stu.writeToParcel(_data, 0);
} else {
_data.writeInt(0);
}
mRemote.transact(Stub.TRANSACTION_callStudent, _data, _reply, 0);
_reply.readException();
} finally {
_reply.recycle();
_data.recycle();
}
}
@Override
public void callClassId(int id) 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(id);
mRemote.transact(Stub.TRANSACTION_callClassId, _data, _reply, 0);
_reply.readException();
} finally {
_reply.recycle();
_data.recycle();
}
}
}
static final int TRANSACTION_callStudent = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
static final int TRANSACTION_callClassId = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);
}
public void callStudent(com.text.aidl.bean.Student stu) throws android.os.RemoteException;
public void callClassId(int id) throws android.os.RemoteException;
}
其实我们在用的时候就是用的 Stub 这个东西
然后这个东西继承了 android.os.Binder 我感觉这个东西就是 把接口aidl化然后在代码中用xxx.sub就可以实现这个接口了
下面是代码结构
图片.png 图片.png注意5.0以后的隐式启动
ComponentName componentName =
new ComponentName("yjapp.yjkm.com.servicedemo", "yjapp.yjkm.com.servicedemo.StudentService");
intent.setComponent(componentName);
bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
要这样
然后服务端要开启服务
Intent intent = new Intent();
ComponentName componentName =
new ComponentName("yjapp.yjkm.com.servicedemo", "yjapp.yjkm.com.servicedemo.StudentService");
intent.setComponent(componentName);
startService(intent);
至于 IClassInterface 中的细节展示就呵呵呵了
网友评论