一.基础知识
官方文档特别提醒我们何时使用AIDL是必要的:只有你允许客户端从不同的应用程序为了进程间的通信而去访问你的service,以及想在你的service处理多线程。(太生硬了,不同进程的组件调用吧。)
那么怎么制作AIDL呢?下面步骤
1:创建.aidl文件。新建立个文件并且以.aidl作 为后缀保存,在这个文件中编写接口以及方法,这个我们普通java接口声明是一样的,不同的是要显示import 复杂类型,即便复杂类型对象在同一个包内。Java基本数据类型 (int, long, char, boolean等),String和CharSequence,集合接口类型List和Map,不需要import 。
比如:
package com.dongzi;
interface IStockQuoteService{
double getPrice(String ticker);
}
2:创建好AIDL文件后,刷新下工程,你会发现在gen包下,对应的包下面出现一个与AIDL文件相同的java文件。如:
/* * This file is auto-generated. DO NOT MODIFY. * Original file: D:\\mywordspace\\MyPhone\\src\\com\\dongzi\\IStockQuoteService.aidl */ package com.dongzi; public interface IStockQuoteService extends android.os.IInterface { /** Local-side IPC implementation stub class. */ public static abstract class Stub extends android.os.Binder implements com.dongzi.IStockQuoteService { private static final java.lang.String DESCRIPTOR = "com.dongzi.IStockQuoteService"; /** Construct the stub at attach it to the interface. */ public Stub() { this.attachInterface(this, DESCRIPTOR); } /** * Cast an IBinder object into an com.dongzi.IStockQuoteService * interface, generating a proxy if needed. */ public static com.dongzi.IStockQuoteService asInterface(android.os.IBinder obj) { if ((obj == null)) { return null; } android.os.IInterface iin = (android.os.IInterface) obj.queryLocalInterface(DESCRIPTOR); if (((iin != null) && (iin instanceof com.dongzi.IStockQuoteService))) { return ((com.dongzi.IStockQuoteService) iin); } return new com.dongzi.IStockQuoteService.Stub.Proxy(obj); } public android.os.IBinder asBinder() { return this; } @Override public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException { switch (code) { case INTERFACE_TRANSACTION: { reply.writeString(DESCRIPTOR); return true; } case TRANSACTION_getPrice: { data.enforceInterface(DESCRIPTOR); java.lang.String _arg0; _arg0 = data.readString(); double _result = this.getPrice(_arg0); reply.writeNoException(); reply.writeDouble(_result); return true; } } return super.onTransact(code, data, reply, flags); } private static class Proxy implements com.dongzi.IStockQuoteService { private android.os.IBinder mRemote; Proxy(android.os.IBinder remote) { mRemote = remote; } public android.os.IBinder asBinder() { return mRemote; } public java.lang.String getInterfaceDescriptor() { return DESCRIPTOR; } public double getPrice(java.lang.String ticker) throws android.os.RemoteException { android.os.Parcel _data = android.os.Parcel.obtain(); android.os.Parcel _reply = android.os.Parcel.obtain(); double _result; try { _data.writeInterfaceToken(DESCRIPTOR); _data.writeString(ticker); mRemote.transact(Stub.TRANSACTION_getPrice, _data, _reply, 0); _reply.readException(); _result = _reply.readDouble(); } finally { _reply.recycle(); _data.recycle(); } return _result; } } static final int TRANSACTION_getPrice = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0); } public double getPrice(java.lang.String ticker) throws android.os.RemoteException; }
AIDL工具自动生成了那么多代码,其实我们只需要知道3个就够了。
public static abstract class Stub extends android.os.Binder implements com.dongzi.IStockQuoteService 静态抽象内部类Stub
private static class Proxy implements com.dongzi.IStockQuoteService AIDL服务代理类
public double getPrice(java.lang.String ticker) throws android.os.RemoteException; AIDL公布出的接口,就是我们定义的接口方法
3:把AIDL文件存放在其他客户端应用中,我们这个作为服务端。当然我们也可以方便的把这个应用作为客户端以及服务端。其实根本区别就是为了使调用者与被调用者在不同进程中,于是在服务中添加android:process=":remote"即可。省去了再建立应用客户端调用。
4:AIDL只是请定义一个契约,我们这里需要一个服务来提供服务。于是建立服务MyService.
二.实战
既然我们了解一些基础知识后,现在我们开始来代码吧。假设我们需要在一个进程中调用其他应用的服务,这个服务提供一个股票价格查询,或者GPS定位的服务。
并且定义一个类,继承我们AIDL生成的Stub内部类,并实现我们AIDL定义的方法
代码如下:
package com.dongzi; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.os.RemoteException; import android.util.Log; public class MyService extends Service { static final String TAG="MyService"; //定义内部类MyServiceImpl继承我们的AIDL文件自动生成的内部类, //并且实现我们AIDL文件定义的接口方法 private class MyServiceImpl extends IStockQuoteService.Stub{ @Override public double getPrice(String ticker) throws RemoteException { Log.e(TAG, "getPrice"); return 10.5; } } @Override public IBinder onBind(Intent arg0) { //返回AIDL实现 return new MyServiceImpl(); } @Override public void onDestroy(){ Log.e(TAG, "Release MyService"); super.onDestroy(); } }
我们需要在onBind方法中返回我们的AIDL接口实现对象,以便其他进程调用。
当然了,现在AIDL,以及Service都定义好了,就需要在mainfest.xml中设置
<service android:name=".MyService" android:process=":remote" > <intent-filter> <action android:name="com.dongzi.IStockQuoteService"/> </intent-filter> </service>
在客户端服务端在同个App中,android:process=":remote",代表在应用程序里,当需要该service时, 会自动创建新的进程。而如果是android:process="remote",没有“:”分号的,则创建全局进程,不同的应用程序共享该进程。
那么现在客户端来调用我们的服务了,代码如下:
package com.dongzi; import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.os.RemoteException; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MYyActivity extends Activity { static final String TAG="MYyActivity"; @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btnCall=(Button)findViewById(R.id.btnCall); if(btnCall!=null) btnCall.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { //绑定一个服务 bindMyService(); } }); } IStockQuoteService iService=null; private ServiceConnection conn=new ServiceConnection(){ @Override public void onServiceConnected(ComponentName name, IBinder service) { //返回AIDL接口对象,然后可以调用AIDL方法 iService=IStockQuoteService.Stub.asInterface(service); double value=0.0; try { value=iService.getPrice(""); } catch (RemoteException e) { Log.e(TAG,"调用出错!"); e.printStackTrace(); } Log.e(TAG, "返回数值为:"+value); } @Override public void onServiceDisconnected(ComponentName name) { Log.i(TAG, "释放Service"); } }; private void bindMyService(){ // Intent intent=new Intent("com.dongzi.IStockQuoteService"); Intent intent=new Intent(this,MyService.class); startService(intent); bindService(intent, conn, Context.BIND_AUTO_CREATE); } }
在按钮点击时候启动service,然后再绑定这个Service.在连接到服务后,我们会发现,调用AIDL中定义的方法成了,打印如下:
项目结构为:
相关推荐
Android aidl 实现进程间通信(IPC)
Android AIDL使用例子
Android AIDL demo (Android Studio)
android aidl文件不一致引起的调用出错问题.doc
android aidl源码实例
android AIDL 完整DEMO ,简单易懂
android aidl进程间的通讯,适合入门dome下载使用,在不同应用进程之间的通讯
使用AIDL实现Android进程间通信,Demo包含Service,Client代码。
Android Aidl 包括了服务端和客户端demo,通过Aidl 客户端可以调用服务器中的方法
android aidl activity 调用部分。
android aidl的使用及demoandroid aidl的使用及demo
Android AIDL Binder 实现与详解。此资源实现了 Android AIDL 通信,自定义 AIDL 数据类型。同时演示了定向 Tag 『inout in out』的区别。并且配有博文详细解释相关知识点以及需要注意的细节。
Android AIDL传输对象代码 Parcelable 在AIDL client的Activity中点击传输对象按钮查看日志,就会看到对象里面的内容
Android AIDL 服务端与客户端实现Demo
android aidl跨进程通信例子,简单明了
Android AIDL示例代码, 包含了各种示例、解释等等,值得下载。
andorid 不同进程间的通信, uses the aidl to comple the demo,hope you can learn ,and give me you idear .
Aidldemo 备份一个 用到的时候直接拿出来用 Service 和Activity 是两个独立的Apk