您的位置:首页 > 移动开发 > Android开发

Android使用AIDL远程通信

2013-04-25 22:39 260 查看
假设A应用需要与B应用进行通信,调用B应用中的query(int no)方法,B应用以Service方式向A应用提供服务。需要下面四个步骤:

1> 在B应用中创建*.aidl文件,aidl文件的定义和接口的定义很相类,如:在com.cqrf.remote.aidl包下创建StudentQuery.aidl文件,内容如下:
package com.cqrf.remote.aidl;
//定义aidl通信接口,通过该文件系统会自动生成相应的aidl通信代码
interface StudentQuery {
String query(int no);
}


当完成aidl文件创建后,eclipse会自动在项目的gen目录中同步生成StudnetQuery.java接口文件。接口文件中生成一个Stub的抽象类,里面包括aidl定义的方法,还包括一些其它辅助方法。值得关注的是asInterface(IBinder iBinder),它返回接口类型的实例,对于远程服务调用,远程服务返回给客户端的对象为代理对象,客户端在onServiceConnected(ComponentName name, IBinder service)方法引用该对象时不能直接强转成接口类型的实例,而应该使用asInterface(IBinder
iBinder)进行类型转换。

编写Aidl文件时,需要注意下面几点:
1.接口名和aidl文件名相同。
2.接口和方法前不用加访问权限修饰符public,private,protected等,也不能用final,static。
3.Aidl默认支持的类型包话java基本类型(int、long、boolean等)和(String、List、Map、CharSequence),使用这些类型时不需要import声明。对于List和Map中的元素类型必须是Aidl支持的类型。如果使用自定义类型作为参数或返回值,自定义类型必须实现Parcelable接口。
4.自定义类型和AIDL生成的其它接口类型在aidl描述文件中,应该显式import,即便在该类和定义的包在同一个包中。
5.在aidl文件中所有非Java基本类型参数必须加上in、out、inout标记,以指明参数是输入参数、输出参数还是输入输出参数。
6.Java原始类型默认的标记为in,不能为其它标记。

2> 在B应用中实现aidl文件生成的接口,但并非直接实现接口,而是通过继承接口的Stub来实现(Stub抽象类内部实现了aidl接口),并且实现接口方法的代码。

3> 在B应用中创建一个Service(服务),在服务的onBind(Intent intent)方法中返回实现了aidl接口的对象(本例是ServiceBinder)。内容如下:

package com.cqrf.remote.service;

import com.cqrf.remote.aidl.StudentQuery.Stub;

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

public class StudentQueryService extends Service {
//定义学生数组
private String[] students = {"张三","李四","李XX","王XX"};
//定义IBinder对象
private IBinder binder = new StudentQueryBinder();
//把IBinder对象返回给调用客户端
public IBinder onBind(Intent intent) {
return binder;
}
/**
* 继承系统自动生成的Stub类,该类实现了IBinder接口,可以通过远程访问该类所定义的方法
* @author Administrator
*
*/
private final class StudentQueryBinder extends Stub{

public String query(int no) throws RemoteException {

return queryStudent(no);
}

}
/**
* 同过NO查询学生
* @param no
* @return 学生姓名
*/
private String queryStudent(int no){
if(no>0 && no <= students.length){
return students[no-1];
}
return null;
}
}


其他应用可以通过隐式意图访问服务,意图的动作和权限都可以自定义,AndroidManifest.xml配置代码如下:

<service android:name="com.cqrf.remote.service.StudentQueryService"
android:permission="cqrf.premission.QUERY_STUDENT">
<intent-filter >
<action android:name="com.cqrf.student.query"/>
</intent-filter>
</service>


4> 把B应用中aidl文件所在package连同aidl文件一起拷贝到客户端A应用,eclipse会自动在A应用的gen目录中为aidl文件同步生成StudentQuery.java接口文件,接下来就可以在A应用中实现与B应用通信,代码如下:

package com.cqrf.remoteservice.client;

import com.cqrf.remote.aidl.StudentQuery;

import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
private EditText noEdit;
private TextView showView;
private StudentQuery studentQuery;
private StudentConn conn = new StudentConn();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
noEdit = (EditText)findViewById(R.id.studentNo);
showView = (TextView)findViewById(R.id.showView);
//通过隐式意图开起远程服务
Intent service = new Intent("com.cqrf.student.query");
//当Activity启动的时候自动绑定
bindService(service, conn, BIND_AUTO_CREATE);
}
//查询按钮的单击事件
public void query(View view){
String stuNo = noEdit.getText().toString();
try {
String name = studentQuery.query(Integer.parseInt(stuNo));
showView.setText(name);
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
}

}

/**
* 创建服务连接类
* @author Administrator
*
*/
private final class StudentConn implements ServiceConnection{

public void onServiceConnected(ComponentName name, IBinder service) {
//由于远程服务返回的IBinder对象是一个代理对象,所以需要进行转换
studentQuery = StudentQuery.Stub.asInterface(service);
}

public void onServiceDisconnected(ComponentName name) {
studentQuery = null;
}

}

public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
protected void onDestroy() {
unbindService(conn);
super.onDestroy();
}

}


如果出现java.lang.SecurityException: Binder invocation to
an incorrect interface这个异常,

可能是因为是两个应用的AIDL文件的包名或者类名不一样,全部改为一样的就OK了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: