Android系统中,各应用程序都运行在自己的进程中,进程之间一般无法进行数据交换。
Android调用Service先定义一个远程调用接口,然后为该接口提供一个实现类。
Android访问Service时,不是直接返回Service对象给客户端——Service只是将一个回调对象(IBinder对象)通过onBind()方法返回给客户端。因此Android的AIDL远程接口的实现类就是那个IBinder实现类。
与绑定本地Service不同的是,本地Service的onBind()方法会直接把Service对象本身传给可uhuduandeServiceConnection的onServiceConnected方法的第二个参数。而远程Service的onBind()方法只是将IBinder对象的代理传给客户端的ServiceConnection的onServiceConnected方法的第二个参数。
当客户端获取远程Service的IBinder的对象的代理之后,接下来就可以通过该IBinder对象去回调远程Service的属性或方法了。
Android用AIDL(Android Interface Definition Language)来定义远程接口。
AIDL这种接口定义语言不是一种真正的编程语言,它只是定义两个进程间的通信接口。
- AIDL定义接口的源代码必须以.aidl结尾。
- AIDL接口中用到数据类型,除了基本类型、String、List、Map、CharSequence之外,其他类型都需要导入包,即使它们在同一个包中也需要导包。
.aidl文件
package com.example.xplus;
interface ICat{
String getColor();
double getWeight();
}
定义好以上接口后,ADT会自动在gen/包..目录下生成一个ICat.java接口,改接口包含一个内部类Stub,该内部类实现了IBinder、ICat两个皆苦,该类将作为远程Service的回调类——它实现了IBinder接口,因此可作为onBind()方法的返回值。
package com.example.xplus;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import com.example.xplus.ICat.Stub;
public class AidlService extends Service {
private CatBinder catBinder;
Timer timer = new Timer();
String[] colors = new String[] { "红色", "黄色", "黑色" };
double[] weights = new double[] { 2.3, 3.1, 1.58 };
private String color;
private double weight;
// 继承Stub,也就是实现额ICat接口,并实现了IBinder接口
public class CatBinder extends Stub {
@Override
public String getColor() throws RemoteException {
return color;
}
@Override
public double getWeight() throws RemoteException {
return weight;
}
}
@Override
public void onCreate() {
super.onCreate();
catBinder = new CatBinder();
timer.schedule(new TimerTask() {
@Override
public void run() {
// 随机地改变Service组件内color、weight属性的值。
int rand = (int) (Math.random() * 3);
color = colors[rand];
weight = weights[rand];
System.out.println("--------" + rand);
}
}, 0, 800);
}
@Override
public void onDestroy() {
timer.cancel();
}
@Override
public IBinder onBind(Intent arg0) {
/*
* 返回catBinder对象 在绑定本地Service的情况下,该catBinder对象会直接
* 传给客户端的ServiceConnection对象 的onServiceConnected方法的第二个参数;
* 在绑定远程Service的情况下,只将catBinder对象的代理 传给客户端的ServiceConnection对象
* 的onServiceConnected方法的第二个参数;
*/
return catBinder;
}
}
package com.example.aidlclient;
import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import com.example.xplus.ICat;
public class AidlClient extends Activity {
private ICat catService;
private Button set;
private EditText color, weight;
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
System.out.println("service disconnected----------");
catService = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
catService = ICat.Stub.asInterface(service);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_aidl_client);
set = (Button) findViewById(R.id.button1);
color = (EditText) findViewById(R.id.color);
weight = (EditText) findViewById(R.id.weight);
Intent intent = new Intent();
intent.setAction("com.example.xplus.action.AIDL_SERVICE");
bindService(intent, conn, Service.BIND_AUTO_CREATE);
set.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
color.setText(catService.getColor());
weight.setText(catService.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) {
getMenuInflater().inflate(R.menu.activity_aidl_client, menu);
return true;
}
}