我相信稍微对android了解一点的人都知道Android系统进程之间是不能共享内存的。因此,需要一些机制来实现不同进程之间的数据交互。Android系统采用RPC方法来实现,实现RPC的服务我们称之为AIDL服务(Android Interface Definition Language)服务。
在这里我们将提供服务的一个应用程序称之为服务端,接受服务的一方叫做客户端。你首先得在服务端定义一个aidl文件,如下所示:
1
2
3
4
5
|
// IMyService.aidl
package
com.weiweishen.docs.aidl;
interface
IMyService{
String getValue();
}
|
然后重新编译程序,系统会在gen目录自动生成一个IMyService.java文件,你不需要改动它,源代码如下:
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
|
//IMyService.java
/*
* This file is auto-generated. DO NOT MODIFY.
* Original file: /Users/wei/Documents/work/Docs/src/com/weiweishen/docs/aidl/IMyService.aidl
*/
package
com.weiweishen.docs.aidl;
public
interface
IMyService
extends
android.os.IInterface
{
/** Local-side IPC implementation stub class. */
public
static
abstract
class
Stub
extends
android.os.Binder
implements
com.weiweishen.docs.aidl.IMyService
{
private
static
final
java.lang.String DESCRIPTOR =
"com.weiweishen.docs.aidl.IMyService"
;
/** Construct the stub at attach it to the interface. */
public
Stub()
{
this
.attachInterface(
this
, DESCRIPTOR);
}
/**
* Cast an IBinder object into an com.weiweishen.docs.aidl.IMyService interface,
* generating a proxy if needed.
*/
public
static
com.weiweishen.docs.aidl.IMyService asInterface(android.os.IBinder obj)
{
if
((obj==
null
)) {
return
null
;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if
(((iin!=
null
)&&(iin
instanceof
com.weiweishen.docs.aidl.IMyService))) {
return
((com.weiweishen.docs.aidl.IMyService)iin);
}
return
new
com.weiweishen.docs.aidl.IMyService.Stub.Proxy(obj);
}
@Override
public
android.os.IBinder asBinder()
{
return
this
;
}
@Override
public
boolean
onTransact(
int
code, android.os.Parcel data, android.os.Parcel reply,
int
flags)
throws
android.os.RemoteException
{
switch
(code)
{
case
INTERFACE_TRANSACTION:
{
reply.writeString(DESCRIPTOR);
return
true
;
}
case
TRANSACTION_getValue:
{
data.enforceInterface(DESCRIPTOR);
java.lang.String _result =
this
.getValue();
reply.writeNoException();
reply.writeString(_result);
return
true
;
}
}
return
super
.onTransact(code, data, reply, flags);
}
private
static
class
Proxy
implements
com.weiweishen.docs.aidl.IMyService
{
private
android.os.IBinder mRemote;
Proxy(android.os.IBinder remote)
{
mRemote = remote;
}
@Override
public
android.os.IBinder asBinder()
{
return
mRemote;
}
public
java.lang.String getInterfaceDescriptor()
{
return
DESCRIPTOR;
}
@Override
public
java.lang.String getValue()
throws
android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
java.lang.String _result;
try
{
_data.writeInterfaceToken(DESCRIPTOR);
mRemote.transact(Stub.TRANSACTION_getValue, _data, _reply,
0
);
_reply.readException();
_result = _reply.readString();
}
finally
{
_reply.recycle();
_data.recycle();
}
return
_result;
}
}
static
final
int
TRANSACTION_getValue = (android.os.IBinder.FIRST_CALL_TRANSACTION +
0
);
}
public
java.lang.String getValue()
throws
android.os.RemoteException;
}
|
这个步骤完成之后,你就可以通过继承IMyService.Stub(继承自Binder类,Android本质上利用Binder实现进程之间的交互)来实现提供服务的具体方法。
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
|
//IMyService.java
package
com.weiweishen.docs;
import
android.app.Service;
import
android.content.Intent;
import
android.os.IBinder;
import
android.os.RemoteException;
import
com.weiweishen.docs.aidl.IMyService;;
public
class
MyService
extends
Service {
int
i =
1
;
public
MyService() {
}
public
class
MyServiceImpl
extends
IMyService.Stub{
@Override
public
String getValue()
throws
RemoteException {
// TODO Auto-generated method stub
i ++ ;
return
"hello everyone "
+ i;
}
}
@Override
public
IBinder onBind(Intent intent) {
return
new
MyServiceImpl();
}
}
|
我们看上面代码,MyServiceImpl继承了IMyService.Stub。getValue()方法是我们提供给客户端的服务,在这里我们是返回一个字符串给客户端。onBinder方法返回一个Binder类,即我们继承IMyService.stub的MyServiceImpl的内部类。实现了这一步,我们服务器端的代码就写好了,下面我们看看客户端,另建一个android project,将IMyService文件连同包目录拷贝到客户端工程的src目录中,看下图,然后在MainActivity实现跨进程调用。
下面是MainActivity的代码:
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
|
//MainActivity.java
package
com.weiweishen.docsclient;
import
com.weiweishen.docs.aidl.IMyService;
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.view.Menu;
import
android.view.MenuItem;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.Button;
import
android.widget.Toast;
public
class
MainActivity
extends
Activity
implements
OnClickListener{
IMyService myService =
null
;
Button btn1;
Button btn2;
Button btn3;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button)findViewById(R.id.button1);
btn2 =(Button)findViewById(R.id.button2);
btn3 =(Button)findViewById(R.id.button3);
btn1.setOnClickListener(
this
);
btn2.setOnClickListener(
this
);
btn3.setOnClickListener(
this
);
}
private
ServiceConnection serviceConnection =
new
ServiceConnection() {
@Override
public
void
onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
@Override
public
void
onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
myService = IMyService.Stub.asInterface(service);
}
};
@Override
public
boolean
onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return
true
;
}
@Override
public
boolean
onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int
id = item.getItemId();
if
(id == R.id.action_settings) {
return
true
;
}
return
super
.onOptionsItemSelected(item);
}
@Override
public
void
onClick(View v) {
// TODO Auto-generated method stub
switch
(v.getId()) {
case
R.id.button1:
bindService(
new
Intent(
"com.weiweishen.docs.aidl.IMyService"
), serviceConnection, Context.BIND_AUTO_CREATE);
break
;
case
R.id.button2:
try
{
Toast.makeText(MainActivity.
this
, myService.getValue(), Toast.LENGTH_LONG).show();
}
catch
(RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break
;
case
R.id.button3:
unbindService(serviceConnection);
break
;
default
:
break
;
}
}
}
|
这里值得注意的是,服务器端的service必须是开启的,否则会报错。这里,按钮1开启服务,按钮2显示返回的数据,按钮3结束服务。
AIDL的介绍就到这里了,感觉自己对Binder的原理理解还是不太透彻,如果有什么不足,欢迎指正。