Android跨进程(APP)通信(四) - AIDL

Android跨进程(APP)通信(一)- 一些例子
Android跨进程(APP)通信(二) - 非AIDL
Android跨进程(APP)通信(三) - Proxy和Stub模式
Android跨进程(APP)通信(四) - AIDL

什么是AIDL

从该系列的几篇文章里,我们知道了Proxy和Stub模式。其实跨进程通信中所谓的AIDL就是帮我们去实现Proxy和Stub模式,帮我们封装好编码译码功能。底层还是transactonTransact方法的调用。

小例子

做一个经典的Echo程序。程序向服务发送一句话,服务给打印出来。

第一步:定义AIDL接口文件(提供Service的APP)

你的服务提供哪些接口去让别人使用,你要先说清楚,但是此时并不需要给出实现。

  • 新建com.example.aidlechoservice.aidl
  • 新建一个普通文件,命名为IEchoService.aidl
1
2
3
4
5
package com.example.aidlechoservice.aidl;

interface {
String echo(String inStr);
}

如果使用Eclipse的话,这样定义之后我们会看到产生了gen/com.example.aidlechoservice.aidl/IEchoService.java文件

第二步:实现服务端的Stub(提供Service的APP)

超级简单,就一句话。

1
2
3
4
5
6
7
IEchoService.Stub mBinder = new IEchoService.Stub() {


public String echo(String inStr) throws RemoteException {
return "echo " + inStr + " at " + sdf.format(new Date());
}
};

所以整体代码就这样

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class EchoService extends Service {

private IEchoService.Stub mBinder;
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


public void onCreate() {
super.onCreate();
mBinder = new IEchoService.Stub() {


public String echo(String inStr) throws RemoteException {
return "echo " + inStr + " at " + sdf.format(new Date());
}
};
}


public IBinder onBind(Intent intent) {
return mBinder;
}

}

第三步:编写APP的AIDL(调用Service的APP)

跟上面服务的是一模一样,必须是一模一样,否则就不行。

第四步:实现客户端的Proxy(调用Service的APP)

很简单,还是一句话搞定

1
IEchoService mService = IEchoService.Stub.asInterface(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
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width= 大专栏  Android跨进程(APP)通信(四) - AIDL4;match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >


<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="bind"
android:text="bind Service" />


<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="echo"
android:text="echo" />


<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="unbind"
android:text="unbind Service" />


</LinearLayout>

代码

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
public class MainActivity extends Activity {

private IEchoService mService;
private ServiceConnection mServiceConnection;
private Intent mServiceIntent = new Intent();


protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mServiceIntent.setComponent(
new ComponentName("com.example.aidlechoservice", "com.example.aidlechoservice.service.EchoService"));
mServiceConnection = new ServiceConnection() {

@Override
public void onServiceDisconnected(ComponentName name) {

}

@Override
public void onServiceConnected(ComponentName name, IBinder binder) {
mService = IEchoService.Stub.asInterface(binder);
}
};
}

public void bind(View v) {
if (isBinded()) {
return;
}
bindService(mServiceIntent, mServiceConnection, Context.BIND_AUTO_CREATE);
}

public void unbind(View v) {
if (!isBinded()) {
return;
}
unbindService(mServiceConnection);
}

public void echo(View v) {
if (!isBinded()) {
return;
}
try {
String result = mService.echo("Hello world!!!");
Log.i("TAG", result);
} catch (RemoteException e) {
e.printStackTrace();
}
}

private boolean isBinded() {
return mService != null;
}
}

项目地址

Github地址

猜你喜欢

转载自www.cnblogs.com/sanxiandoupi/p/11711125.html