`
chenhaodejia
  • 浏览: 116979 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

AIDL(进程间通信)

阅读更多
On Android, one process cannot normally access thememory of another process.

You must define your AIDL interface in an
.
aidl
file using the Javaprogramming language syntax, then save it in the source code (in the
src/
directory) of boththe application hosting the service and any other application that binds to the service.

When you build each application that contains the .aidl file, the Android SDK toolsgenerate an IBinder interface based on the .aidl file and save it inthe project's gen/ directory. The service must implement the IBinderinterface as appropriate. The client applications can then bind to the service and call methods fromthe IBinder to perform IPC.

      To create a bounded service using AIDL, follow these steps:

1.        Create the .aidl file

2.        Implement the interface

3.        Expose the interface to clients



Caution: Any changes that you make to your AIDL interface afteryour first release must remain backward compatible in order to avoid breaking other applicationsthat use your service. That is, because your .aidl file must be copied to other applicationsin order for them to access your service's interface, you must maintain support for the originalinterface.



1.        Create the .aidl file

Each .aidl file must define a single interface and requires only the interface declaration and method signatures.

By default, AIDL supports the following data types:

●     All primitive types in the Java programming language (such as int, long,char, boolean, and so on)

●     String

●     List

●     Map

You must include an import statement for each additional type not listed above, even ifthey are defined in the same package as your interface.

When defining your service interface, be aware that:

●     Methods can take zero or more parameters, and return a value or void.

●     All non-primitive parameters require a directional tag indicating which way the data goes.Either in, out, or inout (see the example below).

●     Primitives are in by default, and cannot be otherwise.

●     All code comments included in the .aidl file are included in the generated IBinder interface (except for comments before the import and packagestatements).

●     Only methods are supported; you cannot expose static fields in AIDL.



Simply save your .aidl file in your project's src/ directory and when youbuild your application, the SDK tools generate the IBinder interface file in yourproject's gen/ directory. The generated file name matches the .aidl file name, butwith a .java extension (for example, IRemoteService.aidl results in IRemoteService.java).

2.        Implement the interface

When you build your application, the Android SDK tools generate a .java interface filenamed after your .aidl file. The generated interface includes a subclass named Stubthat is an abstract implementation of its parent interface (for example, YourInterface.Stub) and declares all the methods from the .aidl file.

Note: Stub also defines a few helper methods, most notably asInterface(), which takes an IBinder (usually the one passed to a client's onServiceConnected() callback method) and returns an instance of the stub interface. See the section Calling an IPCMethod for more details on how to make this cast.



There are a few rules you should be aware of when implementing your AIDL interface:

●     Incoming calls are not guaranteed to be executed on the main thread, so you need to think about multithreading from the start and properly build your service to be thread-safe.

●     By default, RPC calls are synchronous. If you know that the service takes more than a few milliseconds to complete a request, you should not call it from the activity's main thread, because it might hang the application (Android might display an "Application is Not Responding" dialog)—you should usually call them from a separate thread in the client.

●     No exceptions that you throw are sent back to the caller.

3.       Expose the interface to clients

Once you've implemented the interface for your service, you need to expose it

To clients so they can bind to it. To expose the interface for your service, extend Service and implement onBind() to return an instance of your class that implements the generated Stub (as discussed in the previous section). Here's an example service that exposes the IRemoteService example interface to clients.



       Now, when a client (such as an activity) calls bindService() to connect to this service, the client's onServiceConnected() callback receives the mBinder instance returned by the service's onBind()method.

The client must also have access to the interface class, so if the client and service are in separate applications, then the client's application must have a copy of the .aidl file in its src/ directory (which generates the android.os.Binder interface—providing the client access to the AIDL methods).

When the client receives the IBinder in the onServiceConnected() callback, it must callYourServiceInterface.Stub.asInterface(service) to cast the returned parameter to YourServiceInterface type. For example:
分享到:
评论

相关推荐

    AIDL进程间通信demo

    AIDL(Android Interface Definition Language)是Android提供的一种用于实现进程间通信的工具,它允许开发者定义接口,使得服务能够被其他应用调用,即便这些应用运行在不同的进程中。** ### AIDL简介 AIDL是一种...

    AIDL进程间通信

    **AIDL(Android Interface ...总结,AIDL是Android中实现进程间通信的重要工具,它简化了跨进程服务调用的复杂性,提高了应用的交互能力。通过学习和掌握AIDL,开发者可以构建更健壮、功能更丰富的Android应用程序。

    AIDL进程间通信DEMO

    本示例"AIDL进程间通信DEMO"将深入讲解如何使用AIDL来建立两个应用程序组件之间的桥梁。 首先,我们需要了解AIDL的基本概念。AIDL允许开发者定义一个接口,这个接口将在两个进程间被实现和使用。通过AIDL定义的接口...

    aidl进程间通信

    aidl跨进程通信的简单例子,有客户端和服务端。客户端绑定服务,即可发送内容到服务端。如果绑定服务失败,请添加代码:intent.setPackage("com.example.alidservice");即可

    使用 AIDL实现进程间通信

    AIDL(Android Interface Definition Language)是Android提供的一种工具,用于方便开发者实现进程间通信。这篇博客“使用AIDL实现进程间通信”将深入探讨如何利用AIDL进行跨进程的交互。 首先,我们要理解AIDL的...

    AIDL进程间通信(含回调)

    使用AIDL实现进程间通信 两个工程即两个APP,实现数据共享 APP1中有个service 不停的对某一个数进行++操作, APP2通过AIDL 获取APP1的Service中的这个值(通过回调) 当然要使用AIDLAPP1中也要做相应处理

    Android AIDL进程间通信例子代码

    需要注意的是,由于进程间通信涉及到数据的序列化和反序列化,因此AIDL支持的基本数据类型有限,如int、String等。对于复杂对象的传递,需要自定义Parcelable或Serializable接口的实现。 总结来说,AIDL在Android中...

    Android AIDL进程间通信

    综上所述,AIDL是Android平台中实现进程间通信的重要手段,通过合理的使用,可以构建出高效、稳定、可扩展的应用程序。通过阅读和理解给定的`MyTestAIDL`代码资源,开发者可以更好地掌握AIDL的工作方式,为今后的...

    学习aidl进程间通信

    **Android进程间通信(IPC):深入理解AIDL** 在Android系统中,应用程序通常运行在各自的进程中,为了实现不同应用程序间的交互,Android提供了多种进程间通信(IPC, Inter-Process Communication)方式,其中之一...

    android aidl进程间通信

    在Android系统中,AIDL(Android Interface Definition Language)是一种用于实现进程间通信(IPC, Inter-Process Communication)的机制。AIDL允许一个应用服务在不同的进程中运行,使得其他应用能够与之交互,共享...

    高仿360手机卫士,金山手机卫士等手机安全维护软件的大部分功能,已实现来电-短信拦截、手机应用、进程管理、缓存清理等功能,其中使用AIDL进程间通信技术,调用系.zip

    高仿360手机卫士,金山手机卫士等手机安全维护软件的大部分功能,已实现来电-短信拦截、手机应用、进程管理、缓存清理等功能,其中使用AIDL进程间通信技术,调用系.zip

    Android安卓AIDL进程间通信Service简单例子

    总结来说,AIDL是Android系统中用于进程间通信的重要工具,通过它我们可以定义接口并在不同的进程中调用。服务端通过实现AIDL接口并返回`Binder`实例,客户端则通过绑定服务来获取接口并调用方法。理解并熟练掌握...

    AndroidStudio AIDL进程间通信及Binder连接池的实现

    本教程将深入探讨如何使用AIDL和Binder连接池在两个Android应用之间实现进程间通信。 **AIDL基础** AIDL是一种声明式的语言,用于定义服务接口,使得客户端和服务器端可以在不同的进程中调用对方的方法。在Android...

    Android AIDL 进程间通信

    Android进程间通信 AIDL Service 一、服务端结构 服务端需要处理的有: 1.处理对象,对象内方法,注意:AIDL 只能对函数起作用; 2.处理服务; 3.配置文件; 二、客户端 配置 1.对象,切记,与服务端的对象完全一致...

    IPCaidl进程间通信

    文件包含两个工程,Client和Server,通过aidl实现客户端和服务端之间的通信。 服务端和客户端都创建一个aidl文件,定义接口。 服务端通过service将接口暴露给客户端,客户端绑定服务。 解压即可运行。

    Android Aidl进程间通信,实现3个app共享一个服务

    在Android系统中,进程间通信(IPC,Inter-Process Communication)是实现不同应用程序之间数据交换的关键技术。AIDL(Android Interface Definition Language)是Android提供的一个工具,用于在不同进程间定义和...

    android aidl 不同进程间通信

    下面将详细介绍Aidl在不同进程间通信中的应用及其工作原理。 **1. Android进程通信的重要性** Android系统设计时,每个应用运行在自己的进程中,为了保护应用间的隔离性和安全性。但有时我们需要让一个应用调用另一...

Global site tag (gtag.js) - Google Analytics