【Android】Aidl使用详解(支持多个回调和传递自定义对象)
AIDL(Android Interface Definition Language),即Android接口定义语言
Android系统中的进程之间不能共享内存,因此,需要提供一些机制在不同进程之间进行数据通信(即IPC,Inter-Process Communication进程间通信),Binder就是Android中最具特色的IPC方式,AIDL其实就是通过Binder实现的。
以下通过具体示例说明aidl的使用,支持多个回调和传递自定义对象
建立AIDL服务要比建立普通的服务复杂一些,具体步骤如下:
(1)在studio工程的src/main目录中建立一个名为aidl的文件夹,然后在该文件夹中创建.aidl文件,aidl文件的语法类似于Java代码,但会稍有不同。
默认情况下,AIDL 支持下列数据类型:
- Java 编程语言中的所有原语类型(如
int
、long
、char
、boolean
等等) -
String
、CharSequence
-
List
:只支持ArrayList,里面的每个元素都必须被AIDL支持。 -
map
:只支持HashMap, 里面的每个元素都必须被AIDL支持 -
Parcelable
: 所有实现了Parcelable接口的对象 -
AIDL
: 所有的AIDL接口本身也可以在AIDL文件中使用
您必须为以上未列出的每个附加类型加入一个 import 语句,即使这些类型是在与您的接口相同的软件包中定义。示例如下:
package com.demo.sdk.policy;import com.demo.sdk.policy.callback.IMemoryClearCallback;import com.demo.sdk.policy.callback.IMemoryScanCallback;interface IRemoteService { /** * 内存扫描 */ void memoryScan(IMemoryScanCallback iMemoryScanCallback); /** * 内存扫描 */ void memoryClear(IMemoryClearCallback iMemoryClearCallback);}
package com.demo.sdk.policy.callback;import com.demo.sdk.model.AppPackageInfo;import java.util.List;//内存清理回调oneway interface IMemoryClearCallback { /** * 处理中 * @param progress 当前进度 * @param max 总进度值 * @param item 当前kill项 */ void onUpdate(int progress, int max, String item); /** * 处理完成 */ void onResult();}
package com.demo.sdk.policy.callback;import com.demo.sdk.model.AppPackageInfo;import java.util.List;// 内存扫描回调oneway interface IMemoryScanCallback { /** * 扫描引擎准备就绪 */ void onReady(); /** * 扫描中 * @param progress 当前进度 * @param max 总进度值 * @param item 当前扫描项 */ void onUpdate(int progress, int max, String item); /** * 扫描结果 * @param isCancled 是否主动取消 * @param selectedSize 默认选择占用的内存的大小(单位B) * @param size 共扫描出占用内存的大小(单位B) * @param result 扫描结果详情 */ void onResult(boolean isCancled, long selectedSize, long size, in List result);}
注:若接口声明语句中使用了oneway关键字,则该接口中声明的所有方法都采用了oneway方式。oneway 关键字用于修改远程调用的行为。使用该关键字时,远程调用不会阻塞;它只是发送事务数据并立即返回。接口的实现最终接收此调用时,是以正常远程调用形式将其作为来自 Binder 线程池的常规调用进行接收。 如果 oneway 用于本地调用,则不会有任何影响,调用仍是同步调用。
(2)如果aidl文件的内容是正确的,Android SDK 工具基于您的 .aidl
文件,使用 Java 编程语言生成一个接口。此接口具有一个名为 Stub
的内部抽象类,用于扩展 Binder
类并实现 AIDL 接口中的方法。您必须扩展Stub
类并实现方法。也就是建立一个服务类(Service的子类)。
说明:新手来说可能回调部分看不懂,没关系下面会介绍回调,毕竟代码只供参考,目前只要关注unBind()
方法和继承Stub
类就行。
package com.demo.sdk.service;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.os.RemoteCallbackList;import android.os.RemoteException;import android.support.annotation.Nullable;import com.demo.sdk.factory.FlyweightFactory;import com.demo.sdk.model.AppPackageInfo;import com.demo.sdk.constants.Const;import com.demo.sdk.policy.IRemoteService;import com.demo.sdk.policy.callback.IMemoryClearCallback;import com.demo.sdk.policy.callback.IMemoryScanCallback;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.List;/** * aidl调用的服务 */public class RemoteService extends Service { private String TAG = "SDK"; private IRemoteStrviceStub binderStub; private RemoteCallbackList remoteCallbackList = new RemoteCallbackList(); private MemoryManager memoryManager; @Override public void onCreate() { super.onCreate(); binderStub = new IRemoteStrviceStub(); } @Nullable @Override public IBinder onBind(Intent intent) { return binderStub; } public class IRemoteStrviceStub extends IRemoteService.Stub { @Override public void memoryScan(IMemoryScanCallback iMemoryScanCallback) throws RemoteException { synchronized (this) { if (iMemoryScanCallback != null) { remoteCallbackList.register(iMemoryScanCallback, Const.MEMORY_SCAN); } doMemoryScan(); } } @Override public void memoryClear(IMemoryClearCallback iMemoryClearCallback) throws RemoteException { synchronized (this) { if (iMemoryClearCallback != null) { remoteCallbackList.register(iMemoryClearCallback, Const.MEMORY_CLEAR); } doMemoryClear(); } } } /** * 内存扫描 */ private void doMemoryScan(){ if (memoryManager == null) { memoryManager = FlyweightFactory.getManager(getApplicationContext(), MemoryManager.class); } memoryManager.setCallback(new MemoryScanImpl() { @Override public void onReady() { selectCallbackMethod(Const.MEMORY_SCAN, "onReady", null); } @Override public void onUpdate(int progress, int max, String item) { Class[] classes = new Class[]{int.class, int.class, String.class}; selectCallbackMethod(Const.MEMORY_SCAN, "onUpdate", classes, progress, max, item); } @Override public void onResult(boolean isCanceled, long selectedSize, long size, List result) { Class[] classes = new Class[]{boolean.class, long.class, long.class, List.class}; selectCallbackMethod(Const.MEMORY_SCAN, "onResult", classes, isCanceled, selectedSize, size, result); } }, null); memoryManager.scan(); } /** * 内存清理 */ private void doMemoryClear(){ if (memoryManager == null){ return; } memoryManager.setCallback(null, new MemoryClearImpl() { @Override public void onUpdate(int progress, int max, String item) { Class[] classes = new Class[]{int.class, int.class, String.class}; selectCallbackMethod(Const.MEMORY_CLEAR, "onUpdate", classes, progress, max, item); } @Override public void onResult() { selectCallbackMethod(Const.MEMORY_CLEAR, "onResult", null); } }); memoryManager.clear(); } /** * 选择要执行的回调的方法 * @param operateType 回调的cookie(用来确认调用哪一个回调) * @param methodName 方法名称 * @param classes 执行方法的参数类型 * @param args 执行方法的参数 */ public void selectCallbackMethod(String operateType, String methodName,Class[] classes, Object... args) { final int N = remoteCallbackList.beginBroadcast(); for (int i = 0; i < N; i++){ if (remoteCallbackList.getBroadcastCookie(i).equals(operateType)){ invokeMethod(remoteCallbackList.getBroadcastItem(i), methodName, classes, args); } } remoteCallbackList.finishBroadcast(); } /** * 执行方法 * @param obj 执行方法的对象 * @param methodName 方法名称 * @param classes 执行方法的参数类型 * @param args 执行方法的参数 */ public void invokeMethod(Object obj, String methodName, Class[] classes, Object... args){ try { Method method; if (classes != null && classes.length > 0){ method = obj.getClass().getDeclaredMethod(methodName,classes); }else { method = obj.getClass().getDeclaredMethod(methodName); } method.setAccessible(true); method.invoke(obj, args); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } @Override public void onDestroy() { if (remoteCallbackList != null){ remoteCallbackList.kill(); } super.onDestroy(); }}
注:a.由于不能保证在主线程上执行传入调用,因此您一开始就需要做好多线程处理准备,并将您的服务正确地编译为线程安全服务。(代码中使用的是synchronized
,可以用Lock
代替)
b.默认情况下,RPC 调用是同步调用。如果您明知服务完成请求的时间不止几毫秒,就不应该从 Activity 的主线程调用服务,因为这样做可能会使应用挂起(Android 可能会显示“Application is Not Responding”对话框),您通常应该从客户端内的单独线程调用服务。
c.您引发的任何异常都不会回传给调用方。AIDL接口只支持方法,不能声明静态成员;
(3)在AndroidManifest.xml文件中配置AIDL服务,尤其要注意的是,标签中android:name的属性值就是客户端要引用该服务的ID,也就是Intent类的参数值。
建立客户端调用远程服务:
调用类必须执行以下步骤,才能调用使用 AIDL 定义的远程接口:
(1)在项目 src/ main目录中加入 .aidl 文件。(把服务端aidl文件夹完全复制到客户端就行)
(2)a
.声明一个 IBinder 接口实例(基于 AIDL 生成)b
.实现 ServiceConnection。c
.调用 Context.bindService(),以传入您的 ServiceConnection 实现。d
.在您的 onServiceConnected() 实现中,您将收到一个 IBinder 实例(名为 service)。调用YourInterfaceName.Stub.asInterface((IBinder)service),以将返回的参数转换为 YourInterface 类型。示例如下:
package com.example.renkuo.aidldemo;import android.app.Service;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.ServiceConnection;import android.content.pm.PackageManager;import android.content.pm.ResolveInfo;import android.os.IBinder;import android.os.RemoteException;import android.util.Log;import com.demo.sdk.model.AppPackageInfo;import com.demo.sdk.policy.IRemoteService;import com.demo.sdk.policy.callback.IMemoryClearCallback;import com.demo.sdk.policy.callback.IMemoryScanCallback;import java.util.List;public class PolicyAidlServiceWrapper { private Context mContext; private IRemoteService iRemoteService; private static PolicyAidlServiceWrapper policyAidlServiceWrapper = null; public PolicyAidlServiceWrapper(Context context) { mContext = context; // 创建所需绑定的Service的Intent Intent intent = new Intent(); intent.setAction("com.vehiclesafe.sdk.policy.action.AIDL_SERVICE");// intent.setPackage("com.example.renkuo.vehiclesafeaidldemo"); Intent eintent = new Intent(getExplicitIntent(mContext,intent)); // 绑定远程Service boolean binded = mContext.bindService(eintent, conn, Service.BIND_AUTO_CREATE); } public static PolicyAidlServiceWrapper getInstance(Context context) { if (policyAidlServiceWrapper == null) { synchronized (PolicyAidlServiceWrapper.class) { if (policyAidlServiceWrapper == null) { policyAidlServiceWrapper = new PolicyAidlServiceWrapper(context); } } } return policyAidlServiceWrapper; } private ServiceConnection conn = new ServiceConnection() { // Called when the connection with the service is established @Override public void onServiceConnected(ComponentName name, IBinder service) { // 获取远程Service的onBind方法返回的对象的代理 iRemoteService = IRemoteService.Stub.asInterface(service); } // Called when the connection with the service disconnects unexpectedly @Override public void onServiceDisconnected(ComponentName name) { iRemoteService = null; } }; /** * 判断远程服务是否存在 * @return */ public boolean isValid() { return iRemoteService != null; } public static Intent getExplicitIntent(Context context, Intent implicitIntent) { // Retrieve all services that can match the given intent PackageManager pm = context.getPackageManager(); List resolveInfo = pm.queryIntentServices(implicitIntent, 0); // Make sure only one match was found if (resolveInfo == null || resolveInfo.size() != 1) { return null; } // Get component info and create ComponentName ResolveInfo serviceInfo = resolveInfo.get(0); String packageName = serviceInfo.serviceInfo.packageName; String className = serviceInfo.serviceInfo.name; ComponentName component = new ComponentName(packageName, className); // Create a new intent. Use the old one for extras and such reuse Intent explicitIntent = new Intent(implicitIntent); // Set the component to be explicit explicitIntent.setComponent(component); return explicitIntent; } //内存扫描的回调 private IMemoryScanCallback iMemoryScanCallback = new IMemoryScanCallback.Stub(){ @Override public void onReady() throws RemoteException { } @Override public void onUpdate(int progress, int max, String item) throws RemoteException { } @Override public void onResult(boolean isCancled, long selectedSize, long size, List result) throws RemoteException { } }; /** * 执行内存扫描 */ public void executeMemoryScan() { if (!isValid()) return; try { iRemoteService.memoryScan(iMemoryScanCallback); } catch (Exception e) { e.printStackTrace(); } } //内存清理的回调 private IMemoryClearCallback iMemoryClearCallback = new IMemoryClearCallback.Stub(){ @Override public void onUpdate(int progress, int max, String item) throws RemoteException { } @Override public void onResult() throws RemoteException { } }; /** * 执行内存清理 */ public void executeMemoryClear() { if (!isValid()) return; try { iRemoteService.memoryClear(iMemoryClearCallback); } catch (Exception e) { e.printStackTrace(); } }}
(3)调用您在接口上定义的方法。您应该始终捕获 DeadObjectException 异常,它们是在连接中断时引发的;这将是远程方法引发的唯一异常。
(4)如需断开连接,请使用您的接口实例调用 Context.unbindService()。
(5)最后你可以通过客户端调用PolicyAidlServiceWrapper
的executeMemorySca()和executeMemoryClear()方法,直接调用服务端的memoryScan和memoryCleaer方法实现内存扫描和清理的功能。
通过上面代码你可能会有疑问为什么要用getExplicitIntent(Context context, Intent implicitIntent)
方法?
因为Android 5.0一出来后,其中有个特性就是Service Intent must be explitict,也就是说从Lollipop开始,service服务必须采用显示方式启动。源码如下:
171827.png 解决办法参考地址: https://stackoverflow.com/questions/26530565/android-5-0-l-service-intent-must-be-explicit-in-google-analytics传递自定义对象
自定义对象必须实现Parcelable
接口,支持 Parcelable 接口很重要,因为 Android 系统可通过它将对象分解成可编组到各进程的原语。
(1)创建支持 Parcelable 协议的类,您必须执行以下操作:
- 让您的类实现
Parcelable
接口。 - 实现
writeToParcel
,它会获取对象的当前状态并将其写入Parcel
。 - 为您的类添加一个名为
CREATOR
的静态字段,这个字段是一个实现 Parcelable.Creator 接口的对象。
代码示例:
package com.demo.sdk.model.processclear;import android.os.Parcel;import android.os.Parcelable;public class AppPackageInfo implements Parcelable { public String packageName; public String appName; public int usedMemory; public int clearMemory; @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(this.packageName); dest.writeString(this.appName); dest.writeInt(this.usedMemory); dest.writeInt(this.clearMemory); } public AppPackageInfo() { } protected AppPackageInfo(Parcel in) { this.packageName = in.readString(); this.appName = in.readString(); this.usedMemory = in.readInt(); this.clearMemory = in.readInt(); } public static final Creator CREATOR = new Creator() { @Override public AppPackageInfo createFromParcel(Parcel source) { return new AppPackageInfo(source); } @Override public AppPackageInfo[] newArray(int size) { return new AppPackageInfo[size]; } };}
(2)创建一个声明的 .aidl 文件在aidl文件夹下,保持包名路径名一致
package com.demo.sdk.model.processclear;parcelable AppPackageInfo;
(3)客户端和服务端的Parcelable
实现类的.java文件和.aidl文件的类名和包路径必须保证一致。也就是说如果服务端(客户端)创建完的Parcelable
文件完全复制到客户端(服务端)即可
(4)传递自定义对象的时候要用in
关键字修饰
void onResult(boolean isCancled, long selectedSize, long size, in List result);
支持多个回调(参考RemoteServicel类)
(1)通过RemoteCallbackList
的 register(IInterface callback)和register(IInterface callback,Object cookie)方法注册回调,示例采用的是register(IInterface callback,Object cookie)方法,在多个回调时可以通过cookie判断是哪个回调(可以通过unregister(IInterface callback)注销回调)
remoteCallbackList.register(iMemoryScanCallback, Const.MEMORY_SCAN);
(2)aidl通过广播的方式去调用回调方法,代码如下:(其中RemoteCallbackList的getBroadcastItem(int index)方法可以获取对应的回调类,然后再通过invokeMethod方法执行回调的对应方法)
/** * 选择要执行的回调的方法 * @param operateType 回调的cookie(用来确认调用哪一个回调) * @param methodName 方法名称 * @param classes 执行方法的参数类型 * @param args 执行方法的参数 */ public void selectCallbackMethod(String operateType, String methodName,Class[] classes, Object... args) { final int N = remoteCallbackList.beginBroadcast(); for (int i = 0; i < N; i++){ if (remoteCallbackList.getBroadcastCookie(i).equals(operateType)){ invokeMethod(remoteCallbackList.getBroadcastItem(i), methodName, classes, args); } } //记得关闭广播 remoteCallbackList.finishBroadcast(); }
(3)在onDestory的时候调用kill方法(kill方法说明:Disable this callback list. All registered callbacks are unregistered and the list is disabled so that future calls to {@link #register} will fail. This should be used when a Service is stopping, to prevent clients from registering callbacks after it is stopped.)
@Override public void onDestroy() { if (remoteCallbackList != null){ remoteCallbackList.kill(); } super.onDestroy(); }
官方参考地址:https://developer.android.com/guide/components/aidl.html
错误不足之处或相关建议欢迎大家评论指出,谢谢!如果觉得内容可以的话麻烦喜欢(♥)一下
更多相关文章
- 消除“Unfortunately, System UI has stopped”的方法
- android 调用系统相机或者系统相册功能时,onActivityResult方法不
- andorid中Html.fromHtml方法
- php做接口+android 请求API接口并展示到ListView例子
- Android C++层内存泄漏检测
- Android禁止横屏竖屏切换的有效方法