AIDL: Android Interface definition language Android定义的远程接口的语言。不是真正的编程语言,只定义两个进程之间的通信接口。语法十分简单。与JAVA定义接口很像。差异:

(1)定义接口的源代码必须以AIDL结尾。

(2)用到的数据类型,除了基本数据类型,String,LIst,Map,CharSequence外其他的全部需要导包。


service端,客户端都需要用platform-tools子目录下面的aidl工具为该接口提供实现。用adt会自动提供实现。

使用

Service服务器端:

(1)src下ICat.aidl

package com.aidlservicedemo;
interface ICat
{
String getColor();
String getWeight();

}

会在gen下生成一个ICat.Java接口,接口包含了一个Stub子类,这个内部类实现了IBinder,ICat两个接口。

这个Stub类会作为远程Service的回调类,实现了IBindler接口,所以可以作为Service的onBindl()的返回值。

(2)实现service子类。并在子类中实现Stub的子类,也就是实现了ICat接口


开发远程AIDL的service类其实就比service多实现了一个接口而已。

Service访问端:

需要注意 接口文件一致,所以包名也要一致。如下图:

Android 进程间通信之AIDL service_客户端

 

 

Android 进程间通信之AIDL service_客户端_02

(1)创建ServiceConnection对象

(2)以ServiceConnect对象作为参数,调用context的bindService绑定远程Service即可。

客户端在ServiceConnection中不能像之前那样直接获取Service的onBind返回的对象,只能收到代理,所以在ServiceConnect的onServiceConnected中需要用如下方法进行

catService = ICat.Stub.asInterface(service);





客户端访问service时,service并不是把service对象返回给客户端。比如绑定时,service返回的是service的代理对象IBundle返回给了客户端。


package com.aidlservicedemo;

import java.util.Timer;
import java.util.TimerTask;

import com.aidlservicedemo.ICat.Stub;

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

public class AidlService extends Service {

private CatBinder catBinder;
private Timer timer;
private String color;
private double weight;

String[] colors = { "红色", "黄色", "黑色" };
double[] weights = { 2.3, 3.1, 1.58 };

private String TAG = "AidlService";
/**
* 在绑定本地Service情况下,该catBinder会直接传递给客户端的-
* ServiceConnection对象的方法onServiceConnected的第二个参数
* 在绑定远程Service的情况下,只讲catBinder的代理传给客户端的
* ServiceConnection对象的onServiceConnected方法的第二个参数
**/
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub

Log.i(TAG, "---- onBind ----");
return catBinder;

}

/** 启动一个定时任务,来动态的改变color,weight的值。 */
@Override
public void onCreate() {
// TODO Auto-generated method stub
Log.i(TAG, "---- onCreate ----");
super.onCreate();
catBinder = new CatBinder();
timer = new Timer();
timer.schedule(new TimerTask() {

@Override
public void run() {
// TODO Auto-generated method stub
// 随机改变color,weight属性
int rand = (int) (Math.random() * 3);
color = colors[rand];
weight = weights[rand];
System.out.println("------rand:" + rand);
}
}, 0, 800);
}

@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
timer.cancel();
}

/** 继承Stub,也就是实现了ICat,IBinder接口 里面两个函数正是ICat接口函数 */
public class CatBinder extends Stub {

@Override
public String getColor() throws RemoteException {
// TODO Auto-generated method stub
return color;
}

@Override
public double getWeight() throws RemoteException {
// TODO Auto-generated method stub
return weight;
}

}
}



package com.aidlservicedemo;

import com.aidlclienddemo.R;

import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

private ICat iCat = null;
private String AidlAction = "com.aidlservicedemo.action.AIDL_SERVICE";
private String TAG = "ServiceConnection";

private ServiceConnection conn = new ServiceConnection() {

@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
Log.i(TAG, "----- onServiceDisconnected ----");
iCat = null;
}

@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
Log.i(TAG, "----- onServiceConnected ----");
iCat = ICat.Stub.asInterface(service);

}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button getButton = (Button) findViewById(R.id.get);
final TextView color = (TextView) findViewById(R.id.color);
final TextView weight = (TextView) findViewById(R.id.weight);

Intent intent = new Intent(AidlAction);
Boolean res = bindService(intent, conn, Service.BIND_AUTO_CREATE);

if (res) {

Log.i(TAG, "----bindService success!---");
} else {
Log.i(TAG, "----bindService failed!---");
}

getButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
color.setText(iCat.getColor());
weight.setText(iCat.getWeight() + "");
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}

@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
this.unbindService(conn);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}