这篇文章主要介绍了Android中如何利用AIDL机制调用远程服务的相关资料,需要的朋友可以参考下
这篇文章主要介绍了Android中如何利用AIDL机制调用远程服务的相关资料,需要的朋友可以参考下
这篇文章主要介绍了Android中如何利用AIDL机制调用远程服务的相关资料,需要的朋友可以参考下
在android中,每个应用程序都有自己的进程,当需要在不同的进程之间传递对象时,该如何实现呢?显然, java中是不支持跨进程内存共享的。因此要传递对象,需要把对象解析成操作系统能够理解的数据格式,以达到跨界对象访问的目的。在javaee中,采用rmi通过序列化传递对象。在android中,则采用aidl(android interface definitionlanguage:接口描述语言)方式实现。
aidl是一种接口定义语言,用于约束两个进程间的通讯规则,供编译器生成代码,实现android设备上的两个进程间通信(ipc)。aidl的ipc机制和ejb所采用的corba很类似,进程之间的通信信息,首先会被转换成aidl协议消息,然后发送给对方,对方收到aidl协议消息后再转换成相应的对象。由于进程之间的通信信息需要双向转换,所以android采用代理类在背后实现了信息的双向转换,代理类由android编译器生成,对开发人员来说是透明的。


服务端:
|
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.aidlpackage com.itheima.aidl.calculate;interface calculateinterface {double docalculate(double a, double b);}//calculateservice.javapackage 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() {@overridepublic double docalculate(double a, double b) throws remoteexception {return a+b;}};@overridepublic ibinder onbind(intent intent) {log.i("test","onbind...");return mbinder;}@overridepublic boolean onunbind(intent intent) {log.i("test","onunbind...");return super.onunbind(intent);}@overridepublic void oncreate() {super.oncreate();log.i("test","oncreate...");}@overridepublic 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-sdkandroid:minsdkversion="14"android:targetsdkversion="19" /><applicationandroid: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.javapackage 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() {@overridepublic void onservicedisconnected(componentname name) {log.i("test","service disconnected...");mservice = null;}@overridepublic void onserviceconnected(componentname name, ibinder service) {log.i("test","service connected...");mservice = calculateinterface.stub.asinterface(service); //获取接口实例}};@overrideprotected 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加载完毕时回调此方法@overridepublic 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);}@overrideprotected void ondestroy() {unbindservice(mserviceconnection); //解绑远程服务super.ondestroy();}} |
运行结果截图:

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