AIDL的使用情况和实例介绍
2017-01-04 14:08
387 查看
再温故下。
原味连接:http://blog.csdn.net/amazing7/article/details/51317253
AIDL是什么?
AIDL (Android Interface Definition Language), Android接口定义语言,Android提供的IPC(Inter Process Communication,进程间通信)的一种独特实现。
什么情况下要使用AIDL
使用AIDL只有在你允许来自不同应用的客户端跨进程通信访问你的service,并且想要在你的service种处理多线程的时候才是必要的。 如果你不需要执行不同应用之间的IPC并发,你应该通过实现Binder建立你的接口,或者如果你想执行IPC,但是不需要处理多线程。那么使用Messenger实现你的接口。
定义一个AIDL接口的步骤
必须在一个.aidl文件中使用Java编程语言语法定义你的AIDL接口,然后在提供service的应用中和任何绑定到这个service的应用中的源代码中(在src目录吓)保存它。 当你编译包含.aidl文件的应用时,Android SDK工具基于这个.aidl文件生成一个IBinder接口,并且把它保存到项目的gen目录吓.service必须恰当的实现这个IBinder接口 之后客户端应用可以绑定到这个服务上,然后从IBinder调用方法来执行IPC。
使用AIDL建立一个邻接的service需要遵循下面的步骤:
1. 建立.aidl文件
这个文件使用方法签名定义了语言接口
2.实现这个接口
Android SDk工具基于你的.aidl文件使用java语言生成一个接口 这个接口有一个内部抽象类,叫做Stub,它是继承Binder并且实现你AIDL接口的 你必须继承这个Stub类并且实现这些方法
3.暴露这个接口给客户端
实现一个service并且覆盖onBind()方法返回你的Stub实现类。
你的.aidl文件必须被复制到其他应用程序中来让他们访问你service的接口,你必须维护原始接口的支持(向后兼容)。
用一个实例来分步骤说明
在server项目中建立.aidl文件
AIDL使用一个简单的语法让你声明一个带有一个或者多个带有参数和返回值方法的接口 参数和返回值可以是任何类型,甚至是AIDL生成的接口。
IService.aidl
package com.example.aidl; interface IService { String hello(String name); }1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
在server项目中建立服务类
当你编译你的应用时,Android SDK工具生成一个.java接口文件用你的.aidl文件命名生成的接口包含一个名字为Stub的子类,这是一个它父类的抽象实现,并且声明了.aidl中所有的方法。 Stub也定义了一些辅助的方法,最显著的就是asInterface(),它是用来接收一个IBinder(通常IBinder传递给客户端的onServiceConnected()回调方法)并且返回一个Stub接口的实例 。
一旦你为service实现了接口,你需要把它暴露给客户端,这样他们才能绑定到上面 为了给你的service暴露接口,继承Service并且实现onBind()方法返回一个你实现生成的Stub类。
AIDLService.java
public class AIDLService extends Service { @Override public void onCreate() { super.onCreate(); } @Override public IBinder onBind(Intent intent) { // Return the interface return new IService.Stub() { @Override public String hello(String name) throws RemoteException { // TODO Auto-generated method stub return "hello"+name; } }; }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
在server项目AndroidManifest中申明Service
<service android:name="com.example.service.AIDLService" > <intent-filter> <action android:name="android.intent.action.AIDLService" /> </intent-filter> </service>1
2
3
4
5
6
1
2
3
4
5
6
把server项目中的aidl文件带包拷贝到client项目中(包名要相同)
MainActivity.java
public class MainActivity extends Activity { IService RemoteService; //监听服务 private ServiceConnection mConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { // TODO Auto-generated method stub Log.i("mConnection", service+""); RemoteService = IService.Stub.asInterface(service); try { String s= RemoteService.hello("finch"); Toast.makeText(MainActivity.this, s, Toast.LENGTH_LONG).show(); } catch (RemoteException e) { e.printStackTrace(); } } @Override public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initService(); } //连接服务 private void initService() { Intent i = new Intent( ); i.setAction("android.intent.action.AIDLService"); boolean ret = bindService(i, mConnection, Context.BIND_AUTO_CREATE); } //断开服务 private void releaseService() { unbindService(mConnection); mConnection = null; } @Override protected void onDestroy() { super.onDestroy(); releaseService(); } }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
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
运行结果:
相关文章推荐
- (POJ 3411)Paid Roads <DFS>
- 1106. Lowest Price in Supply Chain (25)
- 1090. Highest Price in Supply Chain (25)
- 1079. Total Sales of Supply Chain (25)
- xxxViewController进行pop时直接crash进main.m,EXC_BAD_ACCESS(code=1,address=0x20)
- mac air又满了,清理硬盘
- 通过sails和阿里大于实现短信验证
- 发现大量的TIME_WAIT解决办法
- Jpanel和container和jframe的区别
- 11. Container With Most Water 双指针问题
- 电脑出现initialization failure:0x0000000c的问题
- 设置glassfish开启自动domain
- AIX 配置网卡
- AI 棋手master 50连胜
- Mybaits利用resultMap实现多对多查询
- 听说三星AI助手名为Bixby 所有预装应用都能享用
- SendEmail
- Integrity constraint violation: 1052 Column 'user_id' in where clause is ambiguous
- poj2010 Moo University - Financial Aid 二分
- 亚马逊的人工智能音箱卷入了一起谋杀案