AIDL简述.docx

上传人:b****5 文档编号:3152578 上传时间:2022-11-18 格式:DOCX 页数:14 大小:80.31KB
下载 相关 举报
AIDL简述.docx_第1页
第1页 / 共14页
AIDL简述.docx_第2页
第2页 / 共14页
AIDL简述.docx_第3页
第3页 / 共14页
AIDL简述.docx_第4页
第4页 / 共14页
AIDL简述.docx_第5页
第5页 / 共14页
点击查看更多>>
下载资源
资源描述

AIDL简述.docx

《AIDL简述.docx》由会员分享,可在线阅读,更多相关《AIDL简述.docx(14页珍藏版)》请在冰豆网上搜索。

AIDL简述.docx

AIDL简述

在Android中,如果我们需要在不同进程间实现通信,就需要用到AIDL技术去完成。

AIDL(AndroidInterfaceDefinitionLanguage)是一种接口定义语言,编译器通过*.aidl文件的描述信息生成符合通信协议的Java代码,我们无需自己去写这段繁杂的代码,只需要在需要的时候调用即可,通过这种方式我们就可以完成进程间的通信工作。

AIDL:

(AndroidInterfaceDefinitionLanguage)是一种接口定义语言,是实现不同应用程序之间进行数据访问的一种规则。

ADIL文件里面定义了具体的规则。

编译器把AIDL文件编译为java文件,方便程序调用。

AIDL定义规则:

1:

AIDL要与Activity分开写,单独定义到一个包里面。

客户端的AIDL文件的包名要与服务器端的包名一致。

2:

AIDL文件的接口名不能使用修饰符来修饰。

3:

AIDL文件只能定义抽象方法。

接下来,我就演示一个操作AIDL的最基本的流程。

首先,我们需要建立一个服务端的工程,如图所以:

 

在IPerson.aidl中我们定义了一个“问候”的方法,代码如下:

[java] viewplaincopy

1.package com.scott.aidl;  

2.interface IPerson {  

3.    String greet(String someone);  

4.}  

在Eclipse插件的帮助下,编译器会自动在gen目录中生成对应的IPerson.java文件,格式化后的代码如下:

[java] viewplaincopy

1.package com.scott.aidl;  

2.  

3.public interface IPerson extends android.os.IInterface {  

4.    /** Local-side IPC implementation stub class. */  

5.    public static abstract class Stub extends android.os.Binder implements com.scott.aidl.IPerson {  

6.          

7.        private static final java.lang.String DESCRIPTOR = "com.scott.aidl.IPerson";  

8.  

9.        /** Construct the stub at attach it to the interface. */  

10.        public Stub() {  

11.            this.attachInterface(this, DESCRIPTOR);  

12.        }  

13.  

14.        /** 

15.         * Cast an IBinder object into an com.scott.aidl.IPerson interface, 

16.         * generating a proxy if needed. 

17.         */  

18.        public static com.scott.aidl.IPerson asInterface(android.os.IBinder obj) {  

19.            if ((obj == null)) {  

20.                return null;  

21.            }  

22.            android.os.IInterface iin = (android.os.IInterface) obj.queryLocalInterface(DESCRIPTOR);  

23.            if (((iin !

= null) && (iin instanceof com.scott.aidl.IPerson))) {  

24.                return ((com.scott.aidl.IPerson) iin);  

25.            }  

26.            return new com.scott.aidl.IPerson.Stub.Proxy(obj);  

27.        }  

28.  

29.        public android.os.IBinder asBinder() {  

30.            return this;  

31.        }  

32.  

33.        @Override  

34.        public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags)  

35.                throws android.os.RemoteException {  

36.            switch (code) {  

37.            case INTERFACE_TRANSACTION:

 {  

38.                reply.writeString(DESCRIPTOR);  

39.                return true;  

40.            }  

41.            case TRANSACTION_greet:

 {  

42.                data.enforceInterface(DESCRIPTOR);  

43.                java.lang.String _arg0;  

44.                _arg0 = data.readString();  

45.                java.lang.String _result = this.greet(_arg0);  

46.                reply.writeNoException();  

47.                reply.writeString(_result);  

48.                return true;  

49.            }  

50.            }  

51.            return super.onTransact(code, data, reply, flags);  

52.        }  

53.  

54.        private static class Proxy implements com.scott.aidl.IPerson {  

55.            private android.os.IBinder mRemote;  

56.  

57.            Proxy(android.os.IBinder remote) {  

58.                mRemote = remote;  

59.            }  

60.  

61.            public android.os.IBinder asBinder() {  

62.                return mRemote;  

63.            }  

64.  

65.            public java.lang.String getInterfaceDescriptor() {  

66.                return DESCRIPTOR;  

67.            }  

68.  

69.            public java.lang.String greet(java.lang.String someone) throws android.os.RemoteException {  

70.                android.os.Parcel _data = android.os.Parcel.obtain();  

71.                android.os.Parcel _reply = android.os.Parcel.obtain();  

72.                java.lang.String _result;  

73.                try {  

74.                    _data.writeInterfaceToken(DESCRIPTOR);  

75.                    _data.writeString(someone);  

76.                    mRemote.transact(Stub.TRANSACTION_greet, _data, _reply, 0);  

77.                    _reply.readException();  

78.                    _result = _reply.readString();  

79.                } finally {  

80.                    _reply.recycle();  

81.                    _data.recycle();  

82.                }  

83.                return _result;  

84.            }  

85.        }  

86.  

87.        static final int TRANSACTION_greet = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);  

88.    }  

89.  

90.    public java.lang.String greet(java.lang.String someone) throws android.os.RemoteException;  

91.}  

该文件的大纲视图如下:

IPerson接口中的抽象内部类Stub继承android.os.Binder类并实现IPerson接口,比较重要的方法是asInterface(IBinder)方法,该方法会将IBinder类型的对象转换成IPerson类型,必要的时候生成一个代理对象返回结果。

接下来就是我们的Service了:

[java] viewplaincopy

1.package com.scott.server;  

2.  

3.import android.app.Service;  

4.import 

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 幼儿教育 > 少儿英语

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1