您的位置:首页 > 移动开发 > Android开发

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;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: