Android AIDL运用总结

来源:互联网 发布:逆战一键瞬狙宏软件 编辑:程序博客网 时间:2024/05/18 15:04
AIDL是用于跨进程通信的描述语言,由于项目中需要将一个Android定位能力封装到小进程中,稍微研究了一下其实现,这里记录下来,留作后用。

一、概述

AIDL跨进程通信一般都是一方(进程A)去启动另一方(进程B)的服务(Service),然后由另一方(进程B)去实现一些启动方(进程A)需要的接口(Interface)并回调接口实现,从而使进程A持有一个代理,并以此代理来满足进程A的功能需求,这里面的接口就需要满足AIDL规范。
如上图所示,进程A的Binder其实是进程B的Service的一个Proxy,两者实现了同一个接口,从而使进程A能够按照接口定义获取进程B提供的能力。


有两点需要仔细说明:

1、这里面的接口定义在AIDL文件中(.aidl),eclipse会根据定义的aidl自动生成java文件,就如同R文件一样。该java类实现了远程调用逻辑,我们在实现类Service中只需要实现Interface.Stub接口即可。
2、这里面定义的接口传递的参数必须是简单类型、String或者实现了Parcelable接口的对象,传递的回调必须是有aidl文件定义的接口。

二、实例
以下就具体实例说明,我们要把一个定位能力封装到小进程里,防止定位能力组件的内部逻辑(比如崩溃等)影响主进程的功能。

2.1 进程间通信接口定义与调用

首先我们定义两进程通信的接口IMyLocateListener.aidl,这样会在gen下自动生成一个类IMyLocateListener.java:
package com.example.demo;import com.example.demo.IResultListener;interface IMyLocateListener {    void start(in IResultListener listener);    void stop();    boolean isStarted();}

然后我们用Intent去调起进程B的Service。
Intent intent = new Intent(mContext, MyLocateService.class);mContext.bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);

进程B的Service返回一个Binder给进程A。
@Override    public IBinder onBind(Intent intent) {        return new MyLocateBinder();    }


由MyLocateBinder实现真正的定位能力,实现IMyLocateListener.Stub接口,这个接口就是我们定义的AIDL文件需要实现的接口:

public class MyLocateBinder extends IMyLocateListener.Stub {        @Override        public void start(IResultListener listener) throws RemoteException {            // 开始定位        }        @Override        public void stop() throws RemoteException {            //停止定位        }        @Override        public boolean isStarted() throws RemoteException {            //判断定位状态            return false;        }    }

进程A绑定进程B的服务成功后,会收到ServiceConnection回调:
private ServiceConnection mServiceConnection = new ServiceConnection() {        public void onServiceConnected(ComponentName name, IBinder service) {            mLocateService = IMyLocateListener.Stub.asInterface(service);            try {                mLocateService.start(mResultListener);            } catch (RemoteException e) {                e.printStackTrace();            }        }        public void onServiceDisconnected(ComponentName name) {        }    };
这里就是维护进程B状态和调用进程B能力的地方了,这里要注意onServiceDisconnected只在进程意外终止比如被系统干掉或者stopSelf会执行,调用unbindService后是没有该回调的。

这样我们就能使用进程B提供的能力了,是不是很easy,进程B出了意外也不会影响进程A工作。

2.2 AIDL传入Listener回调

我们刚刚传入了一个mResultListener,这是个回调,所以我们需要定义aidl文件:
package com.example.demo;import com.example.demo.MyParcelableLocation;/*** * 监听定位结果 * @author qqliu * @date 2015-2-4 下午2:58:35 */interface IResultListener {    /**     * 定位成功回调     * @param      * @return     */    void onReceiveLocation(in MyParcelableLocation location);}
然后实现这个接口,并作为参数传入进程B中。
private IResultListener mResultListener = new IResultListener.Stub() {        @Override        public void onReceiveLocation(MyParcelableLocation location)                throws RemoteException {            // do someting        }    };

需要注意,这个回调是进程B执行的,所以不能执行进程A的UI操作,崩了可不能怪我。


2.3 函数参数
我们看到2.1中用到了一个回调对象参数IResultListener,这个我们在2.2中说了,需要定义为aidl,其实这里还可以传入一些实现了Parcelable接口的实体类对象或者基本类型int/long/float/double等,String也可以。

如果传入的不是基本类型和String,我们就要引用(import)对象的定义:
import com.example.demo.IResultListener;

同样的,我们看到了IResultListener返回了一个实体类对象,这个对象也需要实现Parcelable:
public class MyParcelableLocation implements Parcelable {    public static final Parcelable.Creator<MyParcelableLocation> CREATOR = new Creator<MyParcelableLocation>() {        @Override        public MyParcelableLocation[] newArray(int size) {            return new MyParcelableLocation[size];        }        @Override        public MyParcelableLocation createFromParcel(Parcel source) {            MyParcelableLocation location = new MyParcelableLocation();            //解析            return location;        }    };    @Override    public int describeContents() {        return 0;    }    @Override    public void writeToParcel(Parcel dest, int flags) {        //分拆    }}

并且千万不要忘了定义一个AIDL文件(MyParcelableLocation.aidl)进行申明:
package com.example.demo;parcelable MyParcelableLocation;
不声明,IResultListener接口是无法编译通过的。

三、总结

以上几步都完成后,进程间通信就搞定了,整体而言还是非常方便的,主要要点就是所有涉及进程传输的对象和数据都需要AIDL化,保证通信双方能够理解数据。实现了Parcelable接口的对象也需要单独的AIDL文件进行申明。


注:本文提供的获取跨进程代理方法不全面,也可以通过注册代理,然后查询注册表来获取。


0 0