1、使用AIDLAndroid接口描述语言设计和使用远程接口使用AIDL(Android接口描述语言)设计和使用远程接口文章分类:移动开发 关键字: aidl、android 目录1 使用AIDL(AndRoid接口描述语言)设计和使用远程接口 1.1 使用AIDL实现IPC 1.1.1 创建一个AIDL文件 1.1.2 实现接口 1.1.3 向客户端公开接口 1.1.4 使用parcelables进行参数的值传递 1.2 调用一个IPC方法使用AIDL(AndRoid接口描述语言)设计和使用远程接口Since each application runs in its own process, a
2、nd you can write a service that runs in a different process from your Applications UI, sometimes you need to pass objects between processes. On the Android platform, one process can not normally access the memory of another process. So to talk, they need to decompose their objects into primitives th
3、at the operating system can understand, and marshall the object across that boundary for you.通常每个应用程序都在它自己的进程内运行,但有时需要在进程间传递对象,你可以通过应用程序UI的方式写个运行在一个不同的进程中的 service。在AndRoid平台中,一个进程通常不能访问其他进程中的内存区域。所以,他们需要把对象拆分成操作系统能理解的简单形式,以便伪装成对象跨越边界访问。The code to do that marshalling is tedious to write, so we prov
4、ide the AIDL tool to do it for you.编写这种伪装代码相当的枯燥乏味,好在我们提供了AIDL工具可以来做这件事。AIDL (Android Interface Definition Language) is an IDL language used to generate code that enables two processes on an Android device to talk using interprocess communication (IPC). If you have code in one process (for example,
5、in an Activity) that needs to call methods on an object in another process (for example, a Service), you would use AIDL to generate code to marshall the parameters.AIDL(AndRoid接口描述语言)是一个IDL语言,它可以生成一段代码,可以使在一个AndRoid设备上运行的两个进程使用内部通信进程进行交互。如果你需要在一个进程中(例如:在一个Activity中)访问另一个进程中(例如:一个Service)某个对象的方法,你就可以
6、使用 AIDL来生成这样的代码来伪装传递各种参数。The AIDL IPC mechanism is interface-based, similar to COM or Corba, but lighter weight. It uses a proxy class to pass values between the client and the implementation.AIDL IPC的机制是基于接口的,和COM或Corba类似,但它是轻量级的。它使用代理类在客户端和实现层间传递值。This page includes the following main topics:本页包含以
7、下主题:Implementing IPC Using AIDLCalling an .aidl (IPC) Class使用AIDL实现IPC调用一个AIDL(IPC)类使用AIDL实现IPCFollow these steps to implement an IPC service using AIDL.使用AIDL实现一个IPC有下列步骤:1.Create your .aidl file - This file defines an interface (YourInterface.aidl) that defines the methods and fields available to
8、a client.1、创建你的AIDL文件 - 这个文件定义一个接口(YourInterface.aidl),该接口定义了可供客户端访问的方法和属性。2.Add the .aidl file to your makefile - (the Eclipse plugin manages this for you). Android includes the compiler, called AIDL, in the tools/ directory.2、添加AIDL文件到你的makefile中-(Eclipse plugin可以帮你管理)。AndRoid包括编译器,AIDL调用,这些都能在too
9、ls/directory中找到。3.Implement your interface methods - The AIDL compiler creates an interface in the Java programming language from your AIDL interface. This interface has an inner abstract class named Stub that inherits the interface (and implements a few additional methods necessary for the IPC call
10、). You must create a class that extends YourInterface.Stub and implements the methods you declared in your .aidl file.3、实现接口方法-AIDL编译器从你的AIDL接口中使用JAVA编程语言来创建一个接口。这个接口有一个名为Stub的内部抽象类,它继承接口(并实现供IPC调用的所必需的几个附加方法)。你必须创建一个类来实现该接口。4.Expose your interface to clients - If youre writing a service, you should
11、 extend Service and override getBinder() to returning an instance of your class that implements your interface.4、向客户端开放接口-如果你写个service,你应该扩展该Service并重载getBinder()方法来返回一个实现上述接口的类的实例。编辑 创建一个AIDL文件AIDL is a simple syntax that lets you declare an interface with one or more methods, that can take paramet
12、ers and return values. These parameters and return values can be of any type, even other AIDL-generated interfaces. However, it is important to note that you must import all non-built-in types, even if they are defined in the same package as your interface. Here are the data types that AIDL can supp
13、ort:AIDL语法简单,你可以用来声明一个带一个或多个方法的接口,也可以传递参数和返回值。这些参数和返回值可以是任何类型,甚至是其他的AIDL 生成的接口。然而,值得重视的是你必须导入所有的non-bult-in类型,即使他们已经作为接口在其他包里定义了。下面是些AIDL支持的数据类型:Primitive Java programming language types (int, boolean, etc) No import statement is needed.简单Java编程语言类型(int,boolean等) -不需要import声明。One of the following cl
14、asses (no import statements needed):下面类之一(不需要import声明)Java 代码 1. .String 2. .List - All elements in the List must be one of the types in this list, including other AIDL-generated interfaces 3. and parcelables. List may optionally be used as a generic class (e.g. List). The actual concrete class 4. t
15、hat the other side will receive will always be an ArrayList, although the method will be generated to use the 5. List interface. 6. .List - List 中的所有元素都必须是可支持的类型中的一个,包括其他AIDL生成接口和parcelables。List可以作为泛型类来灵活使用(比如 7. List)。 而实际的接受方的类则总是ArrayList,尽管该方法将被生成来使用List接口。 8. .Map - All elements in the Map mus
16、t be of one of the types in this list, including other AIDL-generated interfaces 9. and parcelables. Generic maps, (e.g. of the form Map are not supported. The actual concrete class 10. that the other side will receive will always be a HashMap,although the method will be generated to use the Map int
17、erface. 11. .Map - Map 中的所有元素都必须是可支持的类型中的一个,包括其他AIDL生成接口和parcelables。泛型化的Maps(比如:Map)不被支持。 12. 而实际的接受方的类则总是 HashMap,尽管该方法将被生成去使用Map接口。 13. .CharSequence - This is useful for the CharSequence types used by TextView and other widget objects. 14. .CharSequence - CharSequence 的作用是可以被TextView和其他Widget对象使
18、用。 .String .List - All elements in the List must be one of the types in this list, including other AIDL-generated interfaces and parcelables. List may optionally be used as a generic class (e.g. List). The actual concrete class that the other side will receive will always be an ArrayList, although t
19、he method will be generated to use the List interface. .List - List中的所有元素都必须是可支持的类型中的一个,包括其他AIDL生成接口和parcelables。List可以作为泛型类来灵活使用(比如 List)。而实际的接受方的类则总是ArrayList,尽管该方法将被生成来使用List接口。.Map - All elements in the Map must be of one of the types in this list, including other AIDL-generated interfaces and p
20、arcelables. Generic maps, (e.g. of the form Map are not supported. The actual concrete class that the other side will receive will always be a HashMap,although the method will be generated to use the Map interface. .Map - Map中的所有元素都必须是可支持的类型中的一个,包括其他AIDL生成接口和parcelables。泛型化的Maps(比如:Map)不被支持。 而实际的接受方
21、的类则总是HashMap,尽管该方法将被生成去使用Map接口。.CharSequence - This is useful for the CharSequence types used by TextView and other widget objects. .CharSequence - CharSequence的作用是可以被TextView和其他Widget对象使用。Other AIDL-generated interfaces, which are always passed by reference. An import statement is always needed for
22、 these. Custom classes that implement the Parcelable protocol and are passed by value. An import statement is always needed for these.其他的AIDL生成接口通过引用方式进行传递。所以import声明是必须的。封装协议实现的自定义的类是值传递的方式。所以import声明也是必须的。Here is the basic AIDL syntax:下面是基本的AIDL语法:Java 代码 1. / My AIDL file, named SomeClass.aidl 2.
23、 / Note that standard comment syntax is respected. 3. / Comments before the import or package statements are not bubbled up 4. / to the generated interface, but comments above interface/method/field 5. / declarations are added to the generated interface. 6. / Include your fully-qualified package sta
24、tement. 7. package com.google.android.sample; 8. / See the list above for which classes need 9. / import statements (hint-most of them) 10. import com.google.android.sample.IAtmService; 11. / Declare the interface. 12. interface IBankAccountService 13. / Methods can take 0 or more parameters, and 14
25、. / return a value or void. 15. int getAccountBalance(); 16. void setOwnerNames(in List names); 17. / Methods can even take other AIDL-defined parameters. 18. BankAccount createAccount(in String name, int startingDeposit, in IAtmService atmService); 19. / All non-Java primitive parameters (e.g., int
26、, bool, etc) require 20. / a directional tag indicating which way the data will go. Available 21. / values are in, out, inout. (Primitives are in by default, and cannot be otherwise). 22. / Limit the direction to what is truly needed, because marshalling parameters 23. / is expensive. 24. int getCus
27、tomerList(in String branch, out String customerList); 25. / My AIDL file, named SomeClass.aidl/ Note that standard comment syntax is respected./ Comments before the import or package statements are not bubbled up/ to the generated interface, but comments above interface/method/field/ declarations ar
28、e added to the generated interface./ Include your fully-qualified package statement.package com.google.android.sample;/ See the list above for which classes need/ import statements (hint-most of them)import com.google.android.sample.IAtmService;/ Declare the interface.interface IBankAccountService /
29、 Methods can take 0 or more parameters, and / return a value or void. int getAccountBalance(); void setOwnerNames(in List names); / Methods can even take other AIDL-defined parameters. BankAccount createAccount(in String name, int startingDeposit, in IAtmService atmService); / All non-Java primitive
30、 parameters (e.g., int, bool, etc) require / a directional tag indicating which way the data will go. Available / values are in, out, inout. (Primitives are in by default, and cannot be otherwise). / Limit the direction to what is truly needed, because marshalling parameters / is expensive. int getCustomerList(in String branch, out String customerList);实现接口AIDL generates an interface file for you with the same name as your .aidl file. If you are using the Eclipse plugin, AIDL will aut
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1