AIDL是Android Interface definition language的缩写;它是一种接口定义语言,用来约束两个进程间通讯(IPC)的规则,供编译器生成代码,实现Android设备上两个进程间通讯(IPC),进程之间通讯的信息,首先会被转换成AIDL协议消息,然后发送给对方,对方收到AIDL协议消息后,再转换成相应的对象,由于进程之间的通信信息需要双向转换,所以Android采用代理在背后实现信息的双向转换,代理类由Android编译器自动生成。
进程间通讯就需要创建两个不同的程序,一个作为服务器端,一个作为客户端
一、首先看下服务器端的目录结构
首先创建一个以.aidl为后缀的文件
package com.example.aidl;
/**
* AIDL 供编译器生成通讯代码
*
* @author Administrator
*
*/
interface StudentQuery {
String QueryStudent(int id);
}
两个应用程序之间就是通过QueryStudent进行数据传递,保存以上代码后编译器会自动在gen下生成以.java为后缀的相同名称文件。
在服务器处理应用请求的数据
package com.example.service;
import com.example.aidl.StudentQuery;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
public class StudentQueryService extends Service {
private String[] names = { "张三", "赵六", "李四", "陈九", "王五" };
private IBinder binder = new StudentQuerBinder();
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
//在客户端得到的Binder具有了远程通讯的能力(代理对象)
return binder;
}
private String query(int number) {
if (number > 0 && number < 5) {
return names[number];
}
return null;
}
/**
* 只要继承StudentQuery.Stub就具有了远程通讯的能力
* @author Administrator
*
*/
private final class StudentQuerBinder extends StudentQuery.Stub {
@Override
public String QueryStudent(int id) throws RemoteException {
// TODO Auto-generated method stub
return query(id);
}
}
}
二、客户端目录结构
两个程序需要通讯,一定要有协议去制约,所以在客户端也需要定义相同的aidl文件
package com.example.aidl;
/**
* AIDL 供编译器生成通讯代码
*
* @author Administrator
*
*/
interface StudentQuery {
String QueryStudent(int id);
}
客户端的代码
package com.example.aidlclient;
import com.example.aidl.StudentQuery;
import android.app.Activity;
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.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private TextView tv_result = null;
private EditText et_number = null;
private StudentQuery studentQuery;
private StudentConnection conn = new StudentConnection();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.btn_result);
btn.setOnClickListener(this);
tv_result = (TextView) findViewById(R.id.tv_result);
et_number = (EditText) findViewById(R.id.et_number);
//激活远程服务
Intent intent = new Intent("com.example.student.query");
//绑定远程服务
bindService(intent, conn, BIND_AUTO_CREATE);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn_result:
String number = et_number.getText().toString().trim();
if(TextUtils.isEmpty(number)){
Toast.makeText(MainActivity.this, "学号不能为空", Toast.LENGTH_LONG);
}
else{
int num = Integer.valueOf(number);
try {
String name = studentQuery.QueryStudent(num);
tv_result.setText("查询到的姓名是:" + name);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
break;
default:
break;
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
unbindService(conn);
super.onDestroy();
}
private final class StudentConnection implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
//把代理对象转换成接口对象
studentQuery = StudentQuery.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
studentQuery = null;
}
}
}
这样两个进程间就可以实现通讯了。
Demo下载