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

android aidl浅析

2015-12-16 10:46 357 查看
1.创建aidl文件

在android studio下 点击项目 new–>AIDL–>aidl file

然后点击 make project(ctrl + F9)后 会在 app\build\generated\source\aidl\debug\com\bskuai\aidltest\aidl*IMyAidlInterface.java*

IMyAidlInterface.java 文件就是ide帮我们自动生成的java文件

2.创建service

package com.bskuai.aidltest;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import com.bskuai.aidltest.aidl.IMyAidlInterface;

public class MyService extends Service {

private final IMyAidlInterface.Stub mBinder = new IMyAidlInterface.Stub() {
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {

}

@Override
public void add() throws RemoteException {
Log.e("tag","add");
}

@Override
public void delete() throws RemoteException {
Log.e("tag","delete");
}
};

public MyService() {
}

@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return mBinder;
}
}


3.绑定Service

private ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
myAidlInterface = IMyAidlInterface.Stub.asInterface(service);

}

@Override
public void onServiceDisconnected(ComponentName name) {
myAidlInterface = null;
}
};

Intent intent = new Intent(this,MyService.class);
this.bindService(intent,serviceConnection, Context.BIND_AUTO_CREATE);


4.配置服务

<service
android:name=".MyService"
android:enabled="true"
android:exported="true"
android:process=":remote"
>
</service>


android:process=”:remote” 让服务运行在 包名:remote 的进程中

以上已经实现不同进程间的通信

5.了解自动生成的IMyAidlInterface.java

在客户端中我们 使用了 myAidlInterface = IMyAidlInterface.Stub.asInterface(service);

asInterface()方法

public static com.bskuai.aidltest.aidl.IMyAidlInterface asInterface(android.os.IBinder obj) {
if ((obj == null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin != null) && (iin instanceof com.bskuai.aidltest.aidl.IMyAidlInterface))) {
return ((com.bskuai.aidltest.aidl.IMyAidlInterface) iin);
}
return new com.bskuai.aidltest.aidl.IMyAidlInterface.Stub.Proxy(obj);
}


如果传进来的obj 为null 直接返回 null 通过 queryLocalInterface 得到IInterface对象 如果 该对象不为null 并且和IMyAidlInterface 是同一类型 强转后返回该对象 否则返回Proxy 代理对象

onTransact()负责对自己定义的接口方法的调用

@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_basicTypes: {
data.enforceInterface(DESCRIPTOR);
int _arg0;
_arg0 = data.readInt();
long _arg1;
_arg1 = data.readLong();
boolean _arg2;
_arg2 = (0 != data.readInt());
float _arg3;
_arg3 = data.readFloat();
double _arg4;
_arg4 = data.readDouble();
java.lang.String _arg5;
_arg5 = data.readString();
this.basicTypes(_arg0, _arg1, _arg2, _arg3, _arg4, _arg5);
reply.writeNoException();
return true;
}
case TRANSACTION_add: {
data.enforceInterface(DESCRIPTOR);
this.add();
reply.writeNoException();
return true;
}
case TRANSACTION_delete: {
data.enforceInterface(DESCRIPTOR);
this.delete();
reply.writeNoException();
return true;
}
}
return super.onTransact(code, data, reply, flags);
}


调用时通过code来匹配 下面的 int值

static final int TRANSACTION_basicTypes = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
static final int TRANSACTION_add = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);
static final int TRANSACTION_delete = (android.os.IBinder.FIRST_CALL_TRANSACTION + 2);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android aidl