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

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

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

aidl全称是Android Interface Definition Language,即接口定义语言,依我理解,这个其实和.net,java里面的webservice相似的,webservice也有个wsdl

Web 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() {

@Override
public 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)
*/
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return 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>


好的,所谓的服务端,已经完成,下面开始客户端的工作。

第四步、将服务端自动生成的Stub类(即aidl文件生成到gen文件夹下面的那个类)打成jar,添加到客户端libs文件夹,这步也可在客户端写一个相同包名下的aidl文件来完成。

第五步、客户端通过aidl调用服务端的支付接口

/**
*
*/
package com.study.aidlclient;

import com.figo.study.aidl.AidlTestInterface;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

/**
* @author figo
*
*/
public class AidlTestClientActivity extends Activity {
private Button btnPay;
AidlTestInterface mAidlTestInterface;
String tag = "AidlTestClientActivity";
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
mAidlTestInterface = AidlTestInterface.Stub.asInterface(service);
try {
Log.i(tag, "onServiceConnected,start to pay!");
new PayTask().execute(1);

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public void onServiceDisconnected(ComponentName className) {
mAidlTestInterface = null;
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_aidltest);
btnPay = (Button) findViewById(R.id.btn_pay);
btnPay.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
pay();

}
});

}

class PayTask extends AsyncTask<Integer, Integer, Boolean> {

@Override
protected Boolean doInBackground(Integer... params) {
// TODO Auto-generated method stub
int result = 0;
try {
result = mAidlTestInterface.pay("1234567890", "上饶鸡腿", 10f);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (result == 1) {
Log.i(tag, "pay successful!");

}
return true;
}

@Override
protected void onPostExecute(Boolean result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
//支付完成取消绑定服务
unbindService(mConnection);
Log.i(tag, "unbindService successful!");

}

};

private void pay() {
Intent intent = new Intent("com.figo.study.aidl.service");
//添加packageName避免Implicit intents with startService are not safe
intent.setPackage("com.figo.study");
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: