android开发步步为营之62:进程间通信之Aidl

android进程之间通信,比如一个app和另外一个app交互,有哪几种方式,主要有1、activity的跳转 2、contentprovider 3、broadcast 4、aidl,个人认为前面3种相对简单,应用场景也不一样。本文研究一下使用aidl进行进程之间的通信。

aidl全称是Android Interface Definition Language,即接口定义语言,依我理解,这个其实和.net,java里面的webservice相似的,webservice也有个wsdlWeb Services Description Language,服务端通过定义接口,自动生成代理类。然后客户端使用这个代理类来调用服务端的相应服务。android aidl是通过绑定服务service来实现的。本文写了个demo,之前一直做第三方支付,这里就模拟第三方支付的场景,支付app(服务端)提供一个aidl支付接口给应用app(客户端)来调用完成支付功能。

第一步:服务端定义支付接口AidlTestInterface.aidl

/** * */package com.figo.study.aidl;/** * @author figo * */interface AidlTestInterface {int pay(String orderId,String productName,float money);}然后看到gen文件夹下面会自动生成stub代理类了。

/* * This file is auto-generated. DO NOT MODIFY. * Original file: E:\\projects\\Study\\src\\com\\figo\\study\\aidl\\AidlTestInterface.aidl */package com.figo.study.aidl;/** * @author figo * */public interface AidlTestInterface extends android.os.IInterface{/** Local-side IPC implementation stub class. */public static abstract class Stub extends android.os.Binder implements com.figo.study.aidl.AidlTestInterface{private static final java.lang.String DESCRIPTOR = "com.figo.study.aidl.AidlTestInterface";/** Construct the stub at attach it to the interface. */public Stub(){this.attachInterface(this, DESCRIPTOR);}/** * Cast an IBinder object into an com.figo.study.aidl.AidlTestInterface interface, * generating a proxy if needed. */public static com.figo.study.aidl.AidlTestInterface asInterface(android.os.IBinder obj){if ((obj==null)) {return null;}android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);if (((iin!=null)&&(iin instanceof com.figo.study.aidl.AidlTestInterface))) {return ((com.figo.study.aidl.AidlTestInterface)iin);}return new com.figo.study.aidl.AidlTestInterface.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_pay:{data.enforceInterface(DESCRIPTOR);java.lang.String _arg0;_arg0 = data.readString();java.lang.String _arg1;_arg1 = data.readString();float _arg2;_arg2 = data.readFloat();int _result = this.pay(_arg0, _arg1, _arg2);reply.writeNoException();reply.writeInt(_result);return true;}}return super.onTransact(code, data, reply, flags);}private static class Proxy implements com.figo.study.aidl.AidlTestInterface{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 int pay(java.lang.String orderId, java.lang.String productName, float money) throws android.os.RemoteException{android.os.Parcel _data = android.os.Parcel.obtain();android.os.Parcel _reply = android.os.Parcel.obtain();int _result;try {_data.writeInterfaceToken(DESCRIPTOR);_data.writeString(orderId);_data.writeString(productName);_data.writeFloat(money);mRemote.transact(Stub.TRANSACTION_pay, _data, _reply, 0);_reply.readException();_result = _reply.readInt();}finally {_reply.recycle();_data.recycle();}return _result;}}static final int TRANSACTION_pay = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);}public int pay(java.lang.String orderId, java.lang.String productName, float money) throws android.os.RemoteException;} 第二步:服务端定义一个service实现支付接口

/** * */package com.figo.study.aidl;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.os.RemoteException;import android.util.Log;/** * @author figo * */public class AidlTestService extends Service {String tag = "AidlTestService";private AidlTestInterface.Stub mBinder = new AidlTestInterface.Stub() {@Overridepublic int pay(String orderId, String productName, float money) throws RemoteException {System.out.print("收到客户端请求的数据:" + orderId + "," + productName + "," + money);Log.i(tag, "收到客户端请求的数据:" + orderId + "," + productName + "," + money);return 1;}};/* (non-Javadoc)* @see android.app.Service#onBind(android.content.Intent)*/@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubreturn mBinder;}} 第三步、服务端注册service

<service android:name=".aidl.AidlTestService" ><intent-filter><action android:name="com.figo.study.aidl.service" /><category android:name="android.intent.category.DEFAULT" /></intent-filter></service> 好的,所谓的服务端,,已经完成,下面开始客户端的工作。

旅行是一种病,当你把身边的人都传染了,

android开发步步为营之62:进程间通信之Aidl

相关文章:

你感兴趣的文章:

标签云: