本文介绍如何使用 Android Interface Definition Language (AIDL) 创建接口实现跨进程通信。包括 AIDL 文件定义、Service 的创建与绑定,以及客户端如何通过 AIDL 接口调用 Service 提供的方法。
一.创建: aidl文件
package com.example.administrator.servicetest.aidl;
// Declare any non-default types here with import statements
interface IMyAidlInterface {
/**
* 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);
String getData();
int getNumber();
}
二.创建service
public class MyService_iadl extends Service{
private MyIADL myIADL=new MyIADL();
public MyService_iadl() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return myIADL;//throw new UnsupportedOperationException("Not yet implemented");
}
public class MyIADL extends IMyAidlInterface.Stub
{
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
}
@Override
public String getData() throws RemoteException {
return "OK";
}
@Override
public int getNumber() throws RemoteException {
return 111;
}
}
}