当前位置: 移动技术网 > IT编程>移动开发>Android > Android中如何利用AIDL机制调用远程服务

Android中如何利用AIDL机制调用远程服务

2019年07月24日  | 移动技术网IT编程  | 我要评论

青树零梦,临时夫妻现状调查,洪湖凯撒迪吧

在android中,每个应用程序都有自己的进程,当需要在不同的进程之间传递对象时,该如何实现呢?显然, java中是不支持跨进程内存共享的。因此要传递对象,需要把对象解析成操作系统能够理解的数据格式,以达到跨界对象访问的目的。在javaee中,采用rmi通过序列化传递对象。在android中,则采用aidl(android interface definitionlanguage:接口描述语言)方式实现。

aidl是一种接口定义语言,用于约束两个进程间的通讯规则,供编译器生成代码,实现android设备上的两个进程间通信(ipc)。aidl的ipc机制和ejb所采用的corba很类似,进程之间的通信信息,首先会被转换成aidl协议消息,然后发送给对方,对方收到aidl协议消息后再转换成相应的对象。由于进程之间的通信信息需要双向转换,所以android采用代理类在背后实现了信息的双向转换,代理类由android编译器生成,对开发人员来说是透明的。

服务端:

//calculateinterface.aidl
package com.itheima.aidl.calculate;
interface calculateinterface {
double docalculate(double a, double b);
}
//calculateservice.java
package com.itheima.myaidl.server;
import com.itheima.aidl.calculate.calculateinterface;
import android.app.service;
import android.content.intent;
import android.os.ibinder;
import android.os.remoteexception;
import android.util.log;
public class calculateservice extends service{
private final calculateinterface.stub mbinder = new calculateinterface.stub() {
@override
public double docalculate(double a, double b) throws remoteexception {
return a+b;
}
};
@override
public ibinder onbind(intent intent) {
log.i("test","onbind...");
return mbinder;
}
@override
public boolean onunbind(intent intent) {
log.i("test","onunbind...");
return super.onunbind(intent);
}
@override
public void oncreate() {
super.oncreate();
log.i("test","oncreate...");
}
@override
public void ondestroy() {
super.ondestroy();
log.i("test","ondestroy...");
}
}
//服务端manifast文件
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itheima.myaidl.server"
android:versioncode="1"
android:versionname="1.0" >
<uses-sdk
android:minsdkversion="14"
android:targetsdkversion="19" />
<application
android:allowbackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/apptheme" >
<activity 
android:name="com.itheima.myaidl.server.mainactivity"
android:configchanges="locale|layoutdirection"
android:theme="@android:style/theme.light.notitlebar"
android:screenorientation="portrait">
<intent-filter>
<action android:name="android.intent.action.main" />
<category android:name="android.intent.category.launcher" />
</intent-filter>
</activity>
<service android:name="com.itheima.myaidl.server.calculateservice">
<intent-filter>
<action android:name="com.itheima.myaidl.server.calculateservice" />
</intent-filter>
</service>
</application>
</manifest>
//客户端
//mainactivity.java
package com.itheima.myaidl.client;
import android.app.activity;
import android.content.componentname;
import android.content.context;
import android.content.intent;
import android.content.serviceconnection;
import android.os.bundle;
import android.os.ibinder;
import android.os.remoteexception;
import android.util.log;
import com.itheima.aidl.calculate.calculateinterface;
public class mainactivity extends activity {
private calculateinterface mservice;
private serviceconnection mserviceconnection = new serviceconnection() {
@override
public void onservicedisconnected(componentname name) {
log.i("test","service disconnected...");
mservice = null;
}
@override
public void onserviceconnected(componentname name, ibinder service) {
log.i("test","service connected...");
mservice = calculateinterface.stub.asinterface(service); //获取接口实例
}
};
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
//绑定远程服务
bundle bundle = new bundle();
intent intent = new intent();
intent.putextras(bundle);
intent.setaction("com.itheima.myaidl.server.calculateservice");
bindservice(intent, mserviceconnection, context.bind_auto_create);
}
//todo activity加载完毕时回调此方法
@override
public void onwindowfocuschanged(boolean hasfocus) {
if(hasfocus){
try{
double result = mservice.docalculate(1, 2);
log.i("test","result===>"+result);
}catch(remoteexception e){
e.printstacktrace();
}
}
super.onwindowfocuschanged(hasfocus);
}
@override
protected void ondestroy() {
unbindservice(mserviceconnection); //解绑远程服务
super.ondestroy();
}
}

运行结果截图:

以上所述是小编给大家介绍的android中如何利用aidl机制调用远程服务的相关知识,希望对大家有所帮助!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网