您的位置:首页 > 移动开发 > Android开发

Android笔记三:Android 接口定义语言 (AIDL)--Binder工作原理分析

2017-06-17 11:48 483 查看

Android 接口定义语言 (AIDL)–Binder工作原理分析

源码如下

/*
* This file is auto-generated.  DO NOT MODIFY.
* Original file: ......\\AidlDemo\\aidlclient\\src\\main\\aidl\\com\\example\\admin\\aidlservice\\IRemoteService.aidl
*/
package com.example.admin.aidlservice;
public interface IRemoteService extends android.os.IInterface
{
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements com.example.admin.aidlservice.IRemoteService
{
private static final java.lang.String DESCRIPTOR = "com.example.admin.aidlservice.IRemoteService";
/** Construct the stub at attach it to the interface. */
public Stub()
{
this.attachInterface(this, DESCRIPTOR);
}
/**
* Cast an IBinder object into an com.example.admin.aidlservice.IRemoteService interface,
* generating a proxy if needed.
*/
public static com.example.admin.aidlservice.IRemoteService asInterface(android.os.IBinder obj)
{
if ((obj==null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof com.example.admin.aidlservice.IRemoteService))) {
return ((com.example.admin.aidlservice.IRemoteService)iin);
}
return new com.example.admin.aidlservice.IRemoteService.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_add:
{
data.enforceInterface(DESCRIPTOR);
com.example.admin.aidlservice.Person _arg0;
if ((0!=data.readInt())) {
_arg0 = com.example.admin.aidlservice.Person.CREATOR.createFromParcel(data);
}
else {
_arg0 = null;
}
java.util.List<com.example.admin.aidlservice.Person> _result = this.add(_arg0);
reply.writeNoException();
reply.writeTypedList(_result);
return true;
}
}
return super.onTransact(code, data, reply, flags);
}
private static class Proxy implements com.example.admin.aidlservice.IRemoteService
{
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;
}
//不是基本类型的参数需要表明方向(in/out...)

@Override public java.util.List<com.example.admin.aidlservice.Person> add(com.example.admin.aidlservice.Person person) throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
java.util.List<com.example.admin.aidlservice.Person> _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
if ((person!=null)) {
_data.writeInt(1);
person.writeToParcel(_data, 0);
}
else {
_data.writeInt(0);
}
mRemote.transact(Stub.TRANSACTION_add, _data, _reply, 0);
_reply.readException();
_result = _reply.createTypedArrayList(com.example.admin.aidlservice.Person.CREATOR);
}
finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
}
static final int TRANSACTION_add = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
}
//不是基本类型的参数需要表明方向(in/out...)

public java.util.List<com.example.admin.aidlservice.Person> add(com.example.admin.aidlservice.Person person) throws android.os.RemoteException;
}


1.首先咋server端创建IRemoteService .Stub

public Stub(){
this.attachInterface(this, DESCRIPTOR);
}


2.在client端调asInterface方法,拿到IRemoteService.Stub.Proxy代理类

public static com.example.admin.aidlservice.IRemoteService asInterface(android.os.IBinder obj)
{
//......省略代码,最终返回IRemoteService.Stub.Proxy
return new com.example.admin.ai
a6ee
dlservice.IRemoteService.Stub.Proxy(obj);
}


3.在client端调用prxy里在aidl里声明的方法,在其中在调用IRemoteService.Stub的transact方法

public java.util.List<com.example.admin.aidlservice.Person> add(com.example.admin.aidlservice.Person person) throws android.os.RemoteException
{
//.......省略代码
mRemote.transact(Stub.TRANSACTION_add, _data, _reply, 0);
//......省略代码
}


server最终调用onTransact方法,根据code来调用具体的方法

@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_add:
{
data.enforceInterface(DESCRIPTOR);
com.example.admin.aidlservice.Person _arg0;
if ((0!=data.readInt())) {
_arg0 = com.example.admin.aidlservice.Person.CREATOR.createFromParcel(data);
}
else {
_arg0 = null;
}
java.util.List<com.example.admin.aidlservice.Person> _result = this.add(_arg0);
reply.writeNoException();
reply.writeTypedList(_result);
return true;
}
}
return super.onTransact(code, data, reply, flags);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android