该工具可以把一个aidl文件转换为一个Java类文件,在该Java类文件中,同时重载了transact()和onTransact()方法,统一了存入包裹和读取包裹参数,从而使设计者可以把注意力放到服务代码本身上。
因为我现在使用android studio开发工具开发android应用程序,所以在编写aidl文件时,还是和用eclipse有一些差别。
首先在服务端新建一个aidl文件:
如上图建了一个名为IPayService的aidl文件。就定义一个函数,注意文件的包名要跟应用程序的包名一致,就是清单配置文件里包名,不然执行下一步没效果
然后:
点击make Project之后,就会生一个java文件了
/*
* This file is auto-generated. DO NOT MODIFY.
* Original file: /home/chenjun/work/asproject/Binder/myserver/src/main/aidl/com/cj/myserver/IPayService.aidl
*/
package com.cj.myserver;
// Declare any non-default types here with import statements
public interface IPayService extends android.os.IInterface
{
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements com.cj.myserver.IPayService
{
private static final java.lang.String DESCRIPTOR = "com.cj.myserver.IPayService";
/** Construct the stub at attach it to the interface. */
public Stub()
{
this.attachInterface(this, DESCRIPTOR);
}
/**
* Cast an IBinder object into an com.cj.myserver.IPayService interface,
* generating a proxy if needed.
*/
public static com.cj.myserver.IPayService asInterface(android.os.IBinder obj)
{
if ((obj==null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof com.cj.myserver.IPayService))) {
return ((com.cj.myserver.IPayService)iin);
}
return new com.cj.myserver.IPayService.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);
int _arg0;
_arg0 = data.readInt();
this.pay(_arg0);
reply.writeNoException();
return true;
}
}
return super.onTransact(code, data, reply, flags);
}
private static class Proxy implements com.cj.myserver.IPayService
{
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;
}
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
@Override public void pay(int money) throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeInt(money);
mRemote.transact(Stub.TRANSACTION_pay, _data, _reply, 0);
_reply.readException();
}
finally {
_reply.recycle();
_data.recycle();
}
}
}
static final int TRANSACTION_pay = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
}
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
public void pay(int money) throws android.os.RemoteException;
}
现在再来看一下重新构造的服务端:
看一下主要代码:
package com.cj.myserver;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.Parcel;
import android.os.RemoteException;
import android.util.Log;
public class MyService extends Service {
private static final String TAG = "test";
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
Log.d(TAG, "onBind: ");
MyBinder binder = new MyBinder();
Log.d(TAG, "onBind: "+binder.toString());
return binder;
}
// class MyBinder extends Binder{
// @Override
// protected boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException {
// switch (code){
// case 1:
// data.enforceInterface("MyService");
// int cost = data.readInt();
// pay(cost);
// break;
// }
// return super.onTransact(code, data, reply, flags);
// }
// }
class MyBinder extends IPayService.Stub{
@Override
public void pay(int money) throws RemoteException {
Log.d(TAG, "pay: "+money);
}
}
/**
* 服务端提供的支付服务
* @param money
*/
// public void pay(int money){
// Log.d(TAG, "pay: "+money);
// }
}
在stub类中还定义了一些int常量,比如TRANSACTION_pay,这些常量与服务函数对应,transact()和onTransact()方法的第一个参数code的值即来源于此。
接下来看客户端怎么用aidl文件
直接将服务端的aidl文件拷贝过来,注意连同包一起拷贝,放在源目录下,然后make project也会自动生成相应的java文件
看一下客户端的重构
package com.cj.binder;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.Parcel;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import com.cj.myserver.IPayService;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "test";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent("com.cj.myserver.myservice");
intent.setPackage("com.cj.myserver");
bindService(intent,serviceConnection,BIND_AUTO_CREATE);
}
public void pay(View v){
// int code = 1;
// Parcel data = Parcel.obtain();
// Parcel reply = Parcel.obtain();
// data.writeInterfaceToken("MyService");
// data.writeInt(10);
//
// try {
// binder.transact(code,data,reply,0);
// } catch (RemoteException e) {
// e.printStackTrace();
// }
// reply.recycle();
// data.recycle();
try {
payService.pay(20);
} catch (RemoteException e) {
e.printStackTrace();
}
}
// private IBinder binder;
private IPayService payService;
private ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
Log.d(TAG, "onServiceConnected: ");
Log.d(TAG, "onServiceConnected: "+iBinder.toString());
// binder=iBinder;
payService = IPayService.Stub.asInterface(iBinder);
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
}
};
}
现在客户端这边将返回的Binder对象转换成了IPayService接口,这里其实IPayService.Stub.Proxy对象。该类将作为客户端程序访问服务端的代理,也实现IPayService接口。
public static com.cj.myserver.IPayService asInterface(android.os.IBinder obj)
{
if ((obj==null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof com.cj.myserver.IPayService))) {
return ((com.cj.myserver.IPayService)iin);
}
return new com.cj.myserver.IPayService.Stub.Proxy(obj);
}
@Override public void pay(int money) throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeInt(money);
mRemote.transact(Stub.TRANSACTION_pay, _data, _reply, 0);
_reply.readException();
}
finally {
_reply.recycle();
_data.recycle();
}
}
}
再看一下log
启动service,点击支付按钮,这里我传入的参数是20,结果服务端输出了20
现在看看aidl工具是不是很方便,aidl帮我们封装了一下Binder对象,使客户端和服务端很方便书写代码。
- 本文已收录于以下专栏: