网友通过本文主要向大家介绍了android aidl,android aidl详解,android studio aidl,android aidl实例,android aidl原理等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
【Android】由浅到深理解AIDL
一、 Binder概述
1.1 为什么要用binder
出于安全性、稳定性和内存管理的考虑,Android的应用和系统服务运行在分离的进程中,但是它们之间需要通信和共享数据避免传统IPC开销和服务拒绝的问题android的库不支持System V 的IPCBinder加入了对象引用的引用计数器,消亡提醒机制。当一个Binder服务没有任何终端引用时,它的所有者可以自动提醒它去处理自己Binder通过UID/PID来分辨发送者和接受者(对于安全很重要)但是:
Binder不支持RPC,只有本地客户端和服务端基于消息通信,不适用于流不符合POSIX标准1.2 binder通信流程
客户端使用服务
进程之间无法进行直接通信,所以通过Binder驱动
客户端和服务端不需要了解binder协议,所以使用代理和存根
客户端不想要知道正在使用IPC,也不关心binder和代理,所以,需要管理对象进行抽象
但是客户端怎样获取它想要通信的服务的handle,只需要问问sevicemanager(Context Manager),服务是否已经注册
最后,我们看下总体的架构
二 、 AIDL示例
使用aidl实现跨进程的加减法
2.1 服务端
新建android工程,创建包com.realize.calc.aidl,新建文件ICalcAIDL.aidl,内容如下
package com.realize.calc.aidl; interface ICalcAIDL { int add(int x , int y); int min(int x , int y ); }
创建包com.realize.lizijun.binder_server,新建服务CalcService.java,内容如下
package com.realize.lizijun.binder_server; import com.realize.calc.aidl.ICalcAIDL; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.os.RemoteException; import android.util.Log; public class CalcService extends Service { private static final String TAG = "server"; public void onCreate() { Log.e(TAG, "onCreate"); } public IBinder onBind(Intent t) { Log.e(TAG, "onBind"); return mBinder; } public void onDestroy() { Log.e(TAG, "onDestroy"); super.onDestroy(); } public boolean onUnbind(Intent intent) { Log.e(TAG, "onUnbind"); return super.onUnbind(intent); } public void onRebind(Intent intent) { Log.e(TAG, "onRebind"); super.onRebind(intent); } private final ICalcAIDL.Stub mBinder = new ICalcAIDL.Stub() { @Override public int add(int x, int y) throws RemoteException { return x + y; } @Override public int min(int x, int y) throws RemoteException { return x - y; } }; }
AndroidManifest.xml中注册服务
<service android:name="com.realize.lizijun.binder_server.CalcService"> <intent-filter> <action android:name="com.realize.calc.aidl"> <category android:name="android.intent.category.DEFAULT"> </category></action></intent-filter> </service>
2.2 客户端
新建android工程,创建包com.realize.calc.aidl,新建文件ICalcAIDL.aidl(与服务端是一样的),内容如下
package com.realize.calc.aidl; interface ICalcAIDL { int add(int x , int y); int min(int x , int y ); }
主activity内容如下
package com.realize.lizijun.binder_client; 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.util.Log; import android.view.View; import android.widget.Toast; import com.realize.calc.aidl.ICalcAIDL; public class MainActivity extends Activity { private static final String TAG = "client"; private ICalcAIDL mCalcAidl; private ServiceConnection mServiceConn = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { Log.e(TAG, "onServiceDisconnected"); mCalcAidl = null; } @Override public void onServiceConnected(ComponentName name, IBinder service) { Log.e(TAG, "onServiceConnected"); mCalcAidl = ICalcAIDL.Stub.asInterface(service); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } /** * 点击BindService按钮时调用 * @param view */ public void bindService(View view) { Intent intent = new Intent(); intent.setAction("com.realize.calc.aidl"); bindService(intent, mServiceConn, Context.BIND_AUTO_CREATE); } /** * 点击unBindService按钮时调用 * @param view */ public void unbindService(View view) { unbindService(mServiceConn); } /** * 点击12+12按钮时调用 * @param view */ public void addInvoked(View view) throws Exception { if (mCalcAidl != null) { int addRes = mCalcAidl.add(12, 12); Toast.makeText(this, addRes + "", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "服务器被异常杀死,请重新绑定服务端", Toast.LENGTH_SHORT) .show(); } } /** * 点击50-12按钮时调用 * @param view */ public void minInvoked(View view) throws Exception { if (mCalcAidl != null) { int addRes = mCalcAidl.min(50, 12); Toast.makeText(this, addRes + "", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "服务器未绑定或被异常杀死,请重新绑定服务端", Toast.LENGTH_SHORT) .show(); } } }
界面如下:
2.3 结果说明
点击BindService之后,服务端执行了onCreate和onBind的方法, 客户端执行onServiceConnected方法然后点击12+12,50-12可以成功的调用服务端的代码并返回正确的结果再点击unBindService, Service调用了onUnbind和onDestory然后点击12+12,50-12,仍然能够看到正确的结果,说明客户端与服务端的连接仍然存在我们通过后台,把服务强制停止掉,可以看到调用了onServiceDisconnected方法,此时,再点击12+12,50-12,就获取不到结果了
三、 分析AIDL生成的接口代码
上面创建ICalcAIDL.aidl之后,在gen目录下回生成文件ICalcAIDL.java, 该文件实现了客户端和服务端的代理(proxy)和存根(stub)
3.1 服务端
服务端代码中调用ICalcAIDL.Stub
private final ICalcAIDL.Stub mBinder = new ICalcAIDL.Stub() { @Override public int add(int x, int y) throws RemoteException { return x + y; } @Override public int min(int x, int y) throws RemoteException { return x - y; } };
而在CalcService.java中,很明显Stub就是Binder的子类
public static abstract class Stub extends android.os.Binder implements com.realize.calc.aidl.ICalcAIDL
接着看看Stub下面的方法onTransact,该方法内部实现了加减法的操作
@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_add: { data.enforceInterface(DESCRIPTOR); int _arg0; _arg0 = data.readInt(); int _arg1; _arg1 = data.readInt(); int _result = this.add(_arg0, _arg1); reply.writeNoException(); reply.writeInt(_result); return true; } case TRANSACTION_min: { data.enforceInterface(DESCRIPTOR); int _arg0; _arg0 = data.readInt(); int _arg1; _arg1 = data.readInt(); int _result = this.min(_arg0, _arg1); reply.writeNoException(); re