- 浏览: 64624 次
- 性别:
- 来自: 北京
文章分类
最新评论
AIDL (Android Interface Definition Language) 是一种接口定义语言,用于生成代码允许Android设备上的两个进程间进程通信(IPC).
如果你需要编写一个进程(比如Activity)访问另一个进程(比如Services)的对象的方法代码,你可以使用AIDL自动生成代码而不用自己配置大量的参数.
AIDL IPC基于接口机制,类似COM,Corba,且更加轻量化.它使用一个代理来在客户和实现间传递值.
Implementing IPC Using AIDL 实现进程通信IPC
分为五个步骤:
1.创建 SomeService.aidl 文件
利用aidl.exe生成接口文件.若你的IDE安装了ADT,将会在gen目录或src相应包中自动根据描述文件生成同名接口文件.否则请手动:
命令行:
adil path\SomeService.adil <CR>
注意:
1.自定义类在aidl描述文件中,即便在同一个包中,也要显式import.
2.在aidl文件中所有非Java原始类型参数必须加上标记:in, out, inout.
3.Java 原始类型默认为in,且不能为其它.
4.Java 原始类型包括为java.lang, java,util包中包括的类.
5.接口名同aidl文件名.
6.接口前不用加访问权限修饰符public ,private, protected等,也不能用final ,static.
接口文件分析:
接口中生成一个Stub的抽象类,里面包括aidl定义的方法.还包括一些其它辅助方法.值得关注的是asInterface(IBinder iBinder),它返回接口的实例.
2.实现接口
接口的实现需继承接口.Stub.并实现Stub类的方法.
下面给出一个使用匿名方式实现的例子.
private final SomeService.Stub binder = new SomeService.Stub(){
public void service(){
//...
}
}
注意:
1.没有异常会正常返回
2.RPC通常比较耗时且是异步的,因此应该在线程中调用RPC服务.
3.只支持方法,不支持静态字段.
3.暴露接口给客户
客户要服务,当然要知道在哪有服务.通常一台服务器可能提供不止一个服务.因些,我们使用
RomoteService来管理所有远程服务.
暴露服务必须继承Service.并实现onBind()方法.
- public class RemoteService extends Service {
- ...
- @Override
- public IBinder onBind(Intent intent) {
- // Select the interface to return. If your service only implements
- // a single interface, you can just return it here without checking
- // the Intent.
- if (SomeService. class .getName().equals(intent.getAction())) {
- return mBinder;
- }
- if (ISecondary. class .getName().equals(intent.getAction())) {
- return mSecondaryBinder;
- }
- return null ;
- }
- /**
- * The SomeService Interface is defined through IDL
- */
- private final SomeService.Stub mBinder = new SomeService.Stub() {
- public void registerCallback(SomeServiceCallback cb) {
- if (cb != null ) mCallbacks.register(cb);
- }
- public void unregisterCallback(SomeServiceCallback cb) {
- if (cb != null ) mCallbacks.unregister(cb);
- }
- };
- /**
- * A secondary interface to the service.
- */
- private final ISecondary.Stub mSecondaryBinder = new ISecondary.Stub() {
- public int getPid() {
- return Process.myPid();
- }
- public void basicTypes( int anInt, long aLong, boolean aBoolean,
- float aFloat, double aDouble, String aString) {
- }
- };
- }
public class RemoteService extends Service { ... @Override public IBinder onBind(Intent intent) { // Select the interface to return. If your service only implements // a single interface, you can just return it here without checking // the Intent. if (SomeService.class.getName().equals(intent.getAction())) { return mBinder; } if (ISecondary.class.getName().equals(intent.getAction())) { return mSecondaryBinder; } return null; } /** * The SomeService Interface is defined through IDL */ private final SomeService.Stub mBinder = new SomeService.Stub() { public void registerCallback(SomeServiceCallback cb) { if (cb != null) mCallbacks.register(cb); } public void unregisterCallback(SomeServiceCallback cb) { if (cb != null) mCallbacks.unregister(cb); } }; /** * A secondary interface to the service. */ private final ISecondary.Stub mSecondaryBinder = new ISecondary.Stub() { public int getPid() { return Process.myPid(); } public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) { } }; }
这样暴露好了服务,接着做什么?当然是调用服务了.
调用之前,有必要了解下 对象打包,类似与Java的对象序列化.
4.使用打包传送参数
如果一个类要使用打包功能(类似对象序列化),要实现如下5个步骤:
4.1 实现 Parcelable接口
4.2 实现 public void writeToParcel(Parcel out) 方法
4.3 实现 public void readFromParcel(Parcel in) 方法
4.4 添加一个静态字段 CREATOR 到实现 Parcelable.Creator 接口的类中
4.5 创建一个aidl文件声明你的可打包的类
示例:
Rect.java
- import android.os.Parcel;
- import android.os.Parcelable;
- public final class Rect implements Parcelable {
- public int left;
- public int top;
- public int right;
- public int bottom;
- public static final Parcelable.Creator<Rect> CREATOR = new Parcelable.Creator<Rect>() {
- public Rect createFromParcel(Parcel in) {
- return new Rect(in);
- }
- public Rect[] newArray( int size) {
- return new Rect[size];
- }
- };
- public Rect() {
- }
- private Rect(Parcel in) {
- readFromParcel(in);
- }
- public void writeToParcel(Parcel out) {
- out.writeInt(left);
- out.writeInt(top);
- out.writeInt(right);
- out.writeInt(bottom);
- }
- public void readFromParcel(Parcel in) {
- left = in.readInt();
- top = in.readInt();
- right = in.readInt();
- bottom = in.readInt();
- }
- }
import android.os.Parcel; import android.os.Parcelable; public final class Rect implements Parcelable { public int left; public int top; public int right; public int bottom; public static final Parcelable.Creator<Rect> CREATOR = new Parcelable.Creator<Rect>() { public Rect createFromParcel(Parcel in) { return new Rect(in); } public Rect[] newArray(int size) { return new Rect[size]; } }; public Rect() { } private Rect(Parcel in) { readFromParcel(in); } public void writeToParcel(Parcel out) { out.writeInt(left); out.writeInt(top); out.writeInt(right); out.writeInt(bottom); } public void readFromParcel(Parcel in) { left = in.readInt(); top = in.readInt(); right = in.readInt(); bottom = in.readInt(); } }
Rect.aidl
- package android.graphics;
- // Declare Rect so AIDL can find it and knows that it implements
- // the parcelable protocol.
- parcelable Rect;
package android.graphics; // Declare Rect so AIDL can find it and knows that it implements // the parcelable protocol. parcelable Rect;
注意:
参数一定不能越界.
5.调用IPC方法
调用IPC方法还有6个步骤:
如果觉得烦,那就尽力弄懂,不然你不晕,我译得也晕..
5.1 声明aidl定义的接口类型引用
5.2 实现 ServiceConnection
5.3 调用 Context.bindService(),传入 ServiceConnection 的实现
5.4
在你的 ServiceConnection.onServiceConnected(),你将得到一个 IBinder 实例(service).
调用 YourInterfaceName.Stub.asInterface((IBinder)service)强制转换
YourInterface 类型.
5.5 调用接口定义的方法.你应该始终小心 DeadObjectException 异常,当连接不成功或中断它就会抛出,这也是远程对象唯一的一个异常.
5.6 断开连接,调用 Context.unbindService().
注解:
你可以使用匿名对象作为参数.
对象是引用计数.
ApiDemos 有个子the Remote Activity 的例子.
- public class RemoteServiceBinding extends Activity {
- /** The primary interface we will be calling on the service. */
- IRemoteService mService = null ;
- /** Another interface we use on the service. */
- ISecondary mSecondaryService = null ;
- Button mKillButton;
- TextView mCallbackText;
- private boolean mIsBound;
- /**
- * Standard initialization of this activity. Set up the UI, then wait
- * for the user to poke it before doing anything.
- */
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super .onCreate(savedInstanceState);
- setContentView(R.layout.remote_service_binding);
- // Watch for button clicks.
- Button button = (Button)findViewById(R.id.bind);
- button.setOnClickListener(mBindListener);
- button = (Button)findViewById(R.id.unbind);
- button.setOnClickListener(mUnbindListener);
- mKillButton = (Button)findViewById(R.id.kill);
- mKillButton.setOnClickListener(mKillListener);
- mKillButton.setEnabled( false );
- mCallbackText = (TextView)findViewById(R.id.callback);
- mCallbackText.setText( "Not attached." );
- }
- /**
- * Class for interacting with the main interface of the service.
- */
- private ServiceConnection mConnection = new ServiceConnection() {
- public void onServiceConnected(ComponentName className,
- IBinder service) {
- // This is called when the connection with the service has been
- // established, giving us the service object we can use to
- // interact with the service. We are communicating with our
- // service through an IDL interface, so get a client-side
- // representation of that from the raw service object.
- mService = IRemoteService.Stub.asInterface(service);
- mKillButton.setEnabled( true );
- mCallbackText.setText( "Attached." );
- // We want to monitor the service for as long as we are
- // connected to it.
- try {
- mService.registerCallback(mCallback);
- } catch (RemoteException e) {
- // In this case the service has crashed before we could even
- // do anything with it; we can count on soon being
- // disconnected (and then reconnected if it can be restarted)
- // so there is no need to do anything here.
- }
- // As part of the sample, tell the user what happened.
- Toast.makeText(RemoteServiceBinding. this , R.string.remote_service_connected,
- Toast.LENGTH_SHORT).show();
- }
- public void onServiceDisconnected(ComponentName className) {
- // This is called when the connection with the service has been
- // unexpectedly disconnected -- that is, its process crashed.
- mService = null ;
- mKillButton.setEnabled( false );
- mCallbackText.setText( "Disconnected." );
- // As part of the sample, tell the user what happened.
- Toast.makeText(RemoteServiceBinding. this , R.string.remote_service_disconnected,
- Toast.LENGTH_SHORT).show();
- }
- };
- /**
- * Class for interacting with the secondary interface of the service.
- */
- private ServiceConnection mSecondaryConnection = new ServiceConnection() {
- public void onServiceConnected(ComponentName className,
- IBinder service) {
- // Connecting to a secondary interface is the same as any
- // other interface.
- mSecondaryService = ISecondary.Stub.asInterface(service);
- mKillButton.setEnabled( true );
- }
- public void onServiceDisconnected(ComponentName className) {
- mSecondaryService = null ;
- mKillButton.setEnabled( false );
- }
- };
- private OnClickListener mBindListener = new OnClickListener() {
- public void onClick(View v) {
- // Establish a couple connections with the service, binding
- // by interface names. This allows other applications to be
- // installed that replace the remote service by implementing
- // the same interface.
- bindService( new Intent(IRemoteService. class .getName()),
- mConnection, Context.BIND_AUTO_CREATE);
- bindService( new Intent(ISecondary. class .getName()),
- mSecondaryConnection, Context.BIND_AUTO_CREATE);
- mIsBound = true ;
- mCallbackText.setText( "Binding." );
- }
- };
- private OnClickListener mUnbindListener = new OnClickListener() {
- public void onClick(View v) {
- if (mIsBound) {
- // If we have received the service, and hence registered with
- // it, then now is the time to unregister.
- if (mService != null ) {
- try {
- mService.unregisterCallback(mCallback);
- } catch (RemoteException e) {
- // There is nothing special we need to do if the service
- // has crashed.
- }
- }
- // Detach our existing connection.
- unbindService(mConnection);
- unbindService(mSecondaryConnection);
- mKillButton.setEnabled( false );
- mIsBound = false ;
- mCallbackText.setText( "Unbinding." );
- }
- }
- };
- private OnClickListener mKillListener = new OnClickListener() {
- public void onClick(View v) {
- // To kill the process hosting our service, we need to know its
- // PID. Conveniently our service has a call that will return
- // to us that information.
- if (mSecondaryService != null ) {
- try {
- int pid = mSecondaryService.getPid();
- // Note that, though this API allows us to request to
- // kill any process based on its PID, the kernel will
- // still impose standard restrictions on which PIDs you
- // are actually able to kill. Typically this means only
- // the process running your application and any additional
- // processes created by that app as shown here; packages
- // sharing a common UID will also be able to kill each
- // other's processes.
- Process.killProcess(pid);
- mCallbackText.setText( "Killed service process." );
- } catch (RemoteException ex) {
- // Recover gracefully from the process hosting the
- // server dying.
- // Just for purposes of the sample, put up a notification.
- Toast.makeText(RemoteServiceBinding. this ,
- R.string.remote_call_failed,
- Toast.LENGTH_SHORT).show();
- }
- }
- }
- };
- // ----------------------------------------------------------------------
- // Code showing how to deal with callbacks.
- // ----------------------------------------------------------------------
- /**
- * This implementation is used to receive callbacks from the remote
- * service.
- */
- private IRemoteServiceCallback mCallback = new IRemoteServiceCallback.Stub() {
- /**
- * This is called by the remote service regularly to tell us about
- * new values. Note that IPC calls are dispatched through a thread
- * pool running in each process, so the code executing here will
- * NOT be running in our main thread like most other things -- so,
- * to update the UI, we need to use a Handler to hop over there.
- */
- public void valueChanged( int value) {
- mHandler.sendMessage(mHandler.obtainMessage(BUMP_MSG, value, 0 ));
- }
- };
- private static final int BUMP_MSG = 1 ;
- private Handler mHandler = new Handler() {
- @Override public void handleMessage(Message msg) {
- switch (msg.what) {
- case BUMP_MSG:
- mCallbackText.setText( "Received from service: " + msg.arg1);
- break ;
- default :
- super .handleMessage(msg);
- }
- }
- };
-
}
相关推荐
与很多其他的基于RPC的解决方案一样,Android使用一种接口定义语言(Interface Definition Language,IDL)来公开服务的接口。我们知道4个Android应用程序组件中的3个(Activity、BroadcastReceiver和...
AIDL:Android Interface Definition Language,即Android接口定义语言。 为了使其他的应用程序也可以访问本应用程序提供的服务,Android系统采用了远程过程调用(Remote Procedure Call,RPC)方式来实现。与很多其他...
AIDL:Android Interface Definition Language,翻译过来就是Android接口定义语言。是用于定义服务器和客户端通信接口的一种描述语言,可以拿来生成用于IPC的代码。所以使用AIDL需要一个服务端和客户端 作用:可以在...
**Android AIDL(Android Interface Definition Language)详解** 在Android系统中,进程间的通信(IPC, Inter-Process Communication)是至关重要的,特别是在开发大型、复杂的应用时。AIDL(Android Interface ...
在Android应用开发中,服务(Service)和AIDL(Android Interface Definition Language)是两个非常重要的组件,它们分别用于实现后台长时间运行的任务以及跨进程通信。接下来,我们将详细探讨这两个概念及其在实际...
Android AIDL(Android Interface Definition Language)是一种基于接口定义语言,用于定义 Android 应用程序之间的接口。AIDL 文件是用于定义服务端和客户端之间的接口,用于实现远程过程调用(RPC)。但是, 当...
Android项目之AidlDemo(简单的aidl的例子)。AIDL是Android中IPC(Inter-Process Communication)方式中的一种,AIDL是Android Interface definition language的缩写。需要的小伙伴自请下载。
本文将主要围绕"深入Android架构(从线程到AIDL)"这一主题展开,详细探讨Android系统中的线程管理、进程间通信(IPC)以及AIDL(Android Interface Definition Language)的应用。 首先,我们来谈谈Android中的线程...
AIDL(Android Interface Definition Language)是Android提供的一种接口定义语言,用于支持跨进程调用方法,从而实现多进程间的通信。 AIDL的基本原理是将接口定义为一个.aidl文件,系统会自动生成对应的Java代码...
AIDL(Android Interface Definition Language)是Android系统提供的一种接口定义语言,用于在不同进程间进行通信,特别是当服务(Service)需要与客户端(如Activity)跨进程交互时。AIDL帮助开发者定义接口,使得...
在Android开发中,MD5加密和AIDL(Android Interface Definition Language)通信是两个重要的技术概念。MD5是一种广泛使用的加密散列函数,而AIDL则是Android系统中用于进程间通信(IPC)的一种机制。 首先,让我们...
在Android开发中,AIDL(Android Interface Definition Language)是一种用于实现进程间通信(IPC, Inter-Process Communication)的工具。本教程将详细讲解如何在Android Studio中创建并使用AIDL,以便不同进程间的...
而AIDL(Android Interface Definition Language)是Android系统提供的一种接口定义语言,用于处理进程间通信(IPC, Inter-Process Communication)。当需要在第三方应用中与WPS Office进行交互时,AIDL就成为了一个...
本篇文章将围绕`aidl`(Android Interface Definition Language)这一Binder的重要工具,深入探讨Binder的基本概念、工作原理以及如何通过aidl实现跨进程通信。 首先,我们需要理解什么是`aidl`。aidl是一种接口...
AIDL允许我们在不同的Android应用程序组件之间定义接口,使得它们可以相互通信,即使这些组件在不同的进程中运行。 **AIDL的基本概念** 1. **接口定义**: AIDL文件本质上是定义了一个接口,其中包含了方法声明。这...
为使应用程序之间能够彼此通信,Android提供了IPC (Inter Process Communication,进程间通信)的一种独特实现: AIDL (Android Interface Definition Language, Android接口定义语言)。 建立两个Android项目,...
AIDL是一种接口定义语言,它允许你在Android应用之间定义接口,以实现进程间的通信(IPC)。当你在服务中定义一个AIDL接口,并在客户端调用这个接口时,系统会自动生成相应的 Binder 类,以处理跨进程的调用。 接...
AIDL是Android Interface definition language的缩写;它是一种接口定义语言,用来约束两个进程间通讯(IPC)的规则,供编译器生成代码,实现Android设备上两个进程间通讯(IPC),进程之间通讯的信息,首先会被转换...
Android Aidl(Android Interface Definition Language)是Android提供的一种强大的IPC机制,它允许两个不同进程之间的服务进行接口调用,实现了类似远程方法调用(RPC,Remote Procedure Call)的效果。下面将详细...
Android AIDL(Android Interface Definition Language)是Android系统提供的一种接口定义语言,用于处理进程间通信(IPC, Inter-Process Communication)。在Android应用开发中,当需要在不同进程间共享数据或者...