当前位置:操作系统 > 安卓/Android >>

Android使用AIDL(接口描述语言)设计和使用远程接口

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, and you can write a service that runs in a different process from your Application's 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 that 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 provide the AIDL tool to do it for you.

编写这种伪装代码相当的枯燥乏味,好在我们提供了AIDL工具可以来做这件事。

AIDL (Android Inte易做图ce 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, 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)某个对象的方法,你就可以使用 AIDL来生成这样的代码来伪装传递各种参数。

The AIDL IPC mechanism is inte易做图ce-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:

本页包含以下主题:

Implementing IPC Using AIDL

Calling an .aidl (IPC) Class

使用AIDL实现IPC

调用一个AIDL(IPC)类


使用AIDL实现IPC

Follow these steps to implement an IPC service using AIDL.

使用AIDL实现一个IPC有下列步骤:

1.Create your .aidl file - This file defines an inte易做图ce (YourInte易做图ce.aidl) that defines the methods and fields available to a client.

1、创建你的AIDL文件 - 这个文件定义一个接口(YourInte易做图ce.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调用,这些都能在tools/directory中找到。

3.Implement your inte易做图ce methods - The AIDL compiler creates an inte易做图ce in the Java programming language from your AIDL inte易做图ce. This inte易做图ce has an inner abstract class named Stub that inherits the inte易做图ce (and implements a few additional methods necessary for the IPC call). You must create a class that extends YourInte易做图ce.Stub and implements the methods you declared in your .aidl file.

3、实现接口方法-AIDL编译器从你的AIDL接口中使用JAVA编程语言来创建一个接口。这个接口有一个名为Stub的内部抽象类,它继承接口(并实现供IPC调用的所必需的几个附加方法)。你必须创建一个类来实现该接口。

4.Expose your inte易做图ce to clients - If you're writing a service, you should extend Service and override getBinder() to returning an instance of your class that implements your inte易做图ce.

4、向客户端开放接口-如果你写个service,你应该扩展该Service并重载getBinder()方法来返回一个实现上述接口的类的实例。
[编辑] 创建一个AIDL文件

AIDL is a 易做图 syntax that lets you declare an inte易做图ce with one or more methods, that can take parameters and return values. These parameters and return values can be of any type, even other AIDL-generated inte易做图ces. 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 inte易做图ce. Here are the data types that AIDL can support:

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 classes (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 inte易做图ces
3. and parcelables. List may optionally be used as a "generic" class (e.g. List<String>). The actual concrete class
4. that the other side will receive will always be an ArrayList, although the method will be generated to use the
5. List inte易做图ce.
6. .List - List 中的所有元素都必须是可支持的类型中的一个,包括其他AIDL生成接口和parcelables。List可以作为泛型类来灵活使用(比如
7. List<String>)。 而实际的接受方的类则总是ArrayList,尽管该方法将被生成来使用List接口。
8. .Map - All elements in the Map must be of one of the types in this list, including other AIDL-generated inte易做图ces
9. and parcelables. Generic maps, (e.g. of the form Map<String,Integer> 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 inte易做图ce.
11. .Map - Map 中的所有元素都必须是可支持的类型中的一个,包括其他AIDL生成接口和parcelables。泛型化的Maps(比如:Map<Stirng,Integer>)不被支持。
12. 而实际的接受方的类则总是 HashMap,尽管该方法将被生成去使用Map接口。
13. .CharSequence - This is useful for the CharSequence types used by TextView and other widget objects.
14. .CharSequence - CharSequence 的作用是可以被TextView和其他Widget对象使用。

.String
.List - All elements in the List must be one of the types in this list, including other AIDL-generated inte易做图ces
and parcelables. List may optionally be used as a "generic" class (e.g. List<String>). The actual concrete class
that the other side will receive will always be an ArrayList, although the method will be generated to use the
List inte易做图ce.
.List - List中的所有元素都必须是可支持的类型中的一个,包括其他AIDL生成接口和parcelables。List可以作为泛型类来灵活使用(比如
List<String>)。而实际的接受方的类则总是ArrayList,尽管该方法将被生成来使用List接口。
.Map - All elements in the Map must be of one of the types in this list, including other AIDL-generated inte易做图ces
and parcelables. Generic maps, (e.g. of the form Map<String,Integer> 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 inte易做图ce.
.Map - Map中的所有元素都必须是可支持的类型中的一个,包括其他AIDL生成接口和parcelables。泛型化的Maps(比如:Map<Stirng,Integer>)不被支持。
而实际的接受方的类则总是HashMap,尽管该方法将被生成去使用Map接口。
.CharSequence - This is useful for the CharSequence types used by TextView and other widget objects.
.CharSequence - CharSequence的作用是可以被TextView和其他Widget对象使用。

 

Other AIDL-generated inte易做图ces, which are always passed by reference. An import statement is always n

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,