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

时间:2022-03-01 07:41:12

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

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

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

服务端:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//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机制调用远程服务

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