Android AIDL
2015-07-28 16:28
218 查看
AIDL:Android Interface Definition Language
利用AIDL,客户端和服务器之间可以顺利的进行进程间通讯(IPC)按照上一章内容android bindService(),如果不需要使用并发的IPC,您应该通过继承Binder来创建您的通讯接口,或者,如果确实需要使用IPC,但是不需要处理多线程,那继承Messenger来实现通讯接口。
只有允许来自不同应用的客户端访问您的IPC Service并且您希望在Service中处理多线程,使用AIDL才是必要的。
AIDL的用法:
Service端:
1.创建一个.aidl文件使用java程序语言的语法声明AIDL interface,然后Android SDK工具会基于这个.aidl文件自动生成一个IBinder的接口并且把它保存到工程的gen文件夹下。
// IRemoteService.aidl package com.example.android; // Declare any non-default types here with import statements /** Example service interface */ interface IRemoteService { /** Request the process ID of this service, to do evil things with it. */ int getPid(); /** Demonstrates some basic types that you can use as parameters * and return values in AIDL. */ void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString); }
2.实现.aidl中的interface
private final IRemoteService.Stub mBinder = new IRemoteService.Stub() { public int getPid(){ return Process.myPid(); } public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) { // Does nothing } };
3.为客户端公开接口
一旦完成了对interface的实现,你需要向客户端公开该实现,这样客户端就可以使用它了。这要发布一个Service,并实现onBinder()方法来返回一个被实现的类的实例,该类继承自生成的Stub类(第二步中的mBinder)。
public class RemoteService extends Service { @Override public void onCreate() { super.onCreate(); } @Override public IBinder onBind(Intent intent) { // Return the interface return mBinder; } private final IRemoteService.Stub mBinder = new IRemoteService.Stub() { public int getPid(){ return Process.myPid(); } public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) { // Does nothing } }; }
客户端(如其他进程的一个activity)
当一个客户端(比如一个activty调用)bindService()方法来和这个服务连接时,这个客户端的onServiceConnected() 方法接收mBinder实例并且通过Service的onBind方法返回。客户端必须访问接口类,如果客户端和Service分属两个不同的应用,客户端应用必须复制Service的.aidl文件到他的src/ (和Service端类似,.aidl文件自动生成一个IBinder的接口并且把它保存到工程的gen文件夹下)。
当客户端在 onServiceConnected() 回调方法中接收IBinder时,它必须调用IRemoteService.Stub.asInterface(service)来与IRemoteService 接口类型保持一致。
IRemoteService mIRemoteService; private ServiceConnection mConnection = new ServiceConnection() { // Called when the connection with the service is established public void onServiceConnected(ComponentName className, IBinder service) { // Following the example above for an AIDL interface, // this gets an instance of the IRemoteInterface, which we can use to call on the service mIRemoteService = IRemoteService.Stub.asInterface(service); } // Called when the connection with the service disconnects unexpectedly public void onServiceDisconnected(ComponentName className) { Log.e(TAG, "Service has unexpectedly disconnected"); mIRemoteService = null; } };
相关文章推荐
- Android中的Handler的post方法详解
- Android—shape画虚线
- Android Studio简单设置
- Android开发之给应用签名打包
- Android项目中引用到其他工程
- Android ViewDragHelper拖拉控制的帮助类
- android中listview点击item不变色的问题
- Android 中国际化时间格式表示方式
- Windows环境下Android Studio v1.0安装教程
- Android编程之ArrayList源码详解及使用
- Android5.0Button字体大小写
- Android推送如何实现思路
- Android RecyclerView
- (4.4.1.6)android ANR产生原因和解决办法
- android杂记1
- Android的ActivityGroup与Fragment
- Android自定义View的探讨
- android 开发问题
- 开源IM项目整理(iOS,Android,服务端server,windows版)
- android Studio 常用快捷键大全