AIDL:Android Interface Definition Language,即Android接口定义语言。
为了使其他的应用程序也可以访问本应用程序提供的服务,Android系统采用了远程过程调用(Remote Procedure Call,RPC)方式来实现。与很多其他的基于RPC的解决方案一样,Android使用一种接口定义语言(Interface Definition Language,IDL)来公开服务的接口。我们知道4个Android应用程序组件中的3个(Activity、BroadcastReceiver和ContentProvider)都可以进行跨进程访问,另外一个Android应用程序组件Service同样可以。因此,可以将这种可以跨进程访问的服务称为AIDL(Android Interface Definition Language)服务。
那么该如何实现AIDL通信呢?
首先新建一个.aidl的文件,然后会自动在R文件下生成一个.java的文件,如图
aidl文件里面需要定义接口,以供其它进程能够访问,
package com.example.animation;
interface service
{
String getBook();
String getBookName();
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return mBinder;
}
service.Stub mBinder=new Stub(){
@Override
public String getBook() throws RemoteException {
// TODO Auto-generated method stub
return "wo";
}
@Override
public String getBookName() throws RemoteException {
// TODO Auto-generated method stub
return "ta";
}
};
上面这段代码就是在Service中需要实现的接口,通过Binder机制来实现数据的传递,此处有Binder机制的详解, http://www.cnblogs.com/innost/archive/2011/01/09/1931456.html
然后就是要在Activity里面去BindService了,
Button btn;
private service myAIDLService;
private MyService.MyBinder iBinder;
private ServiceConnection connection=new ServiceConnection(){
@Override
public void onServiceConnected(ComponentName arg0, IBinder arg1) {
// TODO Auto-generated method stub
myAIDLService=service.Stub.asInterface(arg1);
try{
String result=myAIDLService.getBook();
String name=myAIDLService.getBookName();
Log.i("myServiceActivity", result);
Log.i("myServiceActivity", name);
}catch(Exception e){
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
// TODO Auto-generated method stub
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button) findViewById(R.id.menu);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent bindIntent=new Intent(MyServiceActivity.this,MyService.class);
bindService(bindIntent, connection, BIND_AUTO_CREATE);
}
});
}
别忘了,还要注册哦。
如果想要在其他的应用程序中也要用到这个Service怎么办呢,so easy~
先把上面.aidl文件生成的R文件的包,拷贝到应用程序的src目录下
然后,怎么办呢,看代码
Button btn,btn2;
private service myAIDLService;
private ServiceConnection connection=new ServiceConnection(){
@Override
public void onServiceConnected(ComponentName arg0, IBinder arg1) {
// TODO Auto-generated method stub
myAIDLService=service.Stub.asInterface(arg1);
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
// TODO Auto-generated method stub
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.menu_layout2);
btn=(Button) findViewById(R.id.menu2);
btn2=(Button) findViewById(R.id.menu3);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent bindIntent=new Intent("com.example.animation.service");
bindIntent.setPackage("com.example.animation");
bindService(bindIntent, connection, BIND_AUTO_CREATE);
}
});
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(myAIDLService==null){
Log.i("myAIDLService", "null");
return;
}
String result = null;
String name = null;
try {
result=myAIDLService.getBook();
name = myAIDLService.getBookName();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.i("myServiceActivity22", result);
Log.i("myServiceActivity22", name);
}
});
}