为了实现跨进程通信(interprocess communication 简称 IPC),Android提供了AIDL Service。
AIDL 是一种IDL语言,用于生成可以在Android设备上两个进程之间进行通信的代码
如果在一个进程中药调用另一个进程中对象的操作,就可以使用AIDL生成可序列化的参数。
AIDL是面向接口的
与绑定本地Service不同的是,本地Service的onBind方法会直接把IBinder对象本身传给客户端的ServiceConnection
的onServiceConnected方法的第二个参数。但远程Service的onBind方法只是将Ibinder对象的代理传给客户端。
创建AIDL:
new -> file XXX.aidl
XXX.aidl里只是定义一个接口,语法与Java语法相似 但存在几点差异:
1.AIDL定义接口的源代码必须以.aidl 结尾
2.AIDL用到的数据类型,除了基本类型,String,List Map,CharSequence之外,
其他类型全部需要导包。
package com.XXX.XXX;
interface ICat {
String getColor ();
double getWeight();
}
将接口暴露给客户端:
定义好AIDL接口后,ADT工具会自动在工程gen目录下生成一个ICat.java接口,该接口
里包含一个Stub内部类,给内部类实现了IBinder、ICat两个接口,这个Stub类将会作为
远程Service的回调类----它实现了IBinder接口,因此可以作为Service的onBind方法的返回值。
定义一个Service实现类,该Service的onBind方法所返回的IBinder对象应该是ADT所生成的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 IBinder onBind(Intent arg0)
{
/* 返回catBinder对象
* 在绑定本地Service的情况下,该catBinder对象会直接
* 传给客户端的ServiceConnection对象
* 的onServiceConnected方法的第二个参数;
* 在绑定远程Service的情况下,只将catBinder对象的代理
* 传给客户端的ServiceConnection对象
* 的onServiceConnected方法的第二个参数;
*/
return catBinder; //①
}
@Override
public void onDestroy()
{
timer.cancel();
}
}
CatBinder类继承了ICat.Stub类,就是实现了ICat接口和IBinder接口,所以程序重写onBind方法时返回了该CatBinder
的实例
客户端访问:
需要注意的是,不仅服务端需要AIDL接口,客户端同样需要这个接口,而且是一模一样的。
由于看书的时候没仔细看,结果纠结了两个小时。。。
客户端代码:
public class AidlClient extends Activity
{
private ICat catService;
private Button get;
EditText color, weight;
private ServiceConnection conn = new ServiceConnection()
{
@Override
public void onServiceConnected(ComponentName name
, IBinder service)
{
// 获取远程Service的onBind方法返回的对象的代理
catService = ICat.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name)
{
catService = null;
}
};
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
get = (Button) findViewById(R.id.get);
color = (EditText) findViewById(R.id.color);
weight = (EditText) findViewById(R.id.weight);
// 创建所需绑定的Service的Intent
Intent intent = new Intent();
intent.setAction("com.example.aidlservice.AIDL_SERVICE");
// 绑定远程Service
bindService(intent, conn, Service.BIND_AUTO_CREATE);
get.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
try
{
// 获取、并显示远程Service的状态
color.setText(catService.getColor());
weight.setText(catService.getWeight() + "");
}
catch (RemoteException e)
{
e.printStackTrace();
}
}
});
}
@Override
public void onDestroy()
{
super.onDestroy();
// 解除绑定
this.unbindService(conn);
}
}
Abdroid---44---使用AIDL Service 实现跨进程调用Service
原文地址:http://blog.csdn.net/u013476556/article/details/45894195