一、aidl介绍
1.1
AIDL(Android Interface Definition Language接口描述语言)是一个IDL语言,它可以生成一段代码,可以使在一个android设备上运行的两个进程使用内部通信进程进行交互。
1.2
如果你需要在一个进程中(例如:在一个Activity中)访问另一个进程中(例如:一个Service)某个对象的方法,你就可以使用AIDL来生成这样的代码来伪装传递各种参数。
二、aidl使用
2.1
要使用AIDL,Service需要以aidl文件的方式提供服务接口,AIDL工具将生成一个相应的java接口,并且在生成的服务接口中包含一个功能调用的stub服务桩类。
Service的实现类需要去继承这个stub服务桩类。Service的onBind方法会返回实现类的对象,之后你就可以使用它了。
2.2
交互过程client<-->proxy<-->stub<-->service
stub和proxy是为了方便client/service交互而生成出来的代码,这样client/service的代码就会比较干净,不会嵌入很多很难懂的与业务无关的代码
2.3
使用aidl步骤:
①创建你的aidl文件,我在后面给出了一个例子,它的aidl文件定义如下:
写法跟java代码类似,但是这里有一点值得注意的就是它可以引用其它aidl文件中定义的接口,但是不能够引用你的java类文件中定义的接口
②编译你的aidl文件,这个只要是在eclipse中开发,你的adt插件会像资源文件一样把aidl文件编译成java代码生成在gen文件夹下,不用手动去编译:编译生成如AIDLService.java
③实现你定义aidl接口中的内部抽象类Stub
Stub类继承了Binder,并继承我们在aidl文件中定义的接口
④你怎么在客户端如何调用服务端得aidl描述的接口对象,doc只告诉我们需要实现Service.onBind(Intent)方法,该方法会返回一个IBinder对象到客户端,绑定服务时不是需要一个ServiceConnection对象么,在没有了解aidl用法前一直不知道它是什么作用,其实他就是用来在客户端绑定service时接收service返回的IBinder对象的
2.4 写法介绍
aidl的写法非常简单,步骤如下
在服务端:
创建一个AIDL接口文件(如果用到了其他的类,要将类序列化,并在AIDL文件中声明)
再创建Service用于响应客户端的绑定请求。
接着创建一个类,让这个类继承AIDL接口中的Stub类,并实现其抽象方法。在Service的onBind方法中返回这个新建这个类的对象。
---------------------------------------------------------------------------------------------------------------------------------------
接着在客户端:
在客户中绑定服务端的Service,绑定成功后就可以在ServiceConnection中的onServiceConnected方法中将返回的Binder对象转换成AIDL接口所属的类型。
拿到Binder对象后就可以调用在AIDL文件中声明的方法了。以下是一个简单的示例来说明如何实现 AIDL 接口:
1. 创建 AIDL 接口定义文件
首先,创建一个名为 IMyInterface.aidl 的 AIDL 文件,定义接口方法:
// IMyInterface.aidl
package com.example;
interface IMyInterface {
int add(int a, int b);
}
2. 实现 AIDL 接口
接下来,在你的 Android 项目中创建一个服务并实现定义的接口:
// MyService.java
package com.example;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class MyService extends Service {
private IBinder mBinder = new IMyInterface.Stub() {
@Override
public int add(int a, int b) {
return a + b;
}
};
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
}
3. 在清单文件中注册服务
确保在 AndroidManifest.xml 文件中注册服务:
<service android:name=".MyService">
<intent-filter>
<action android:name="com.example.IMyInterface" />
</intent-filter>
</service>
4. 在客户端调用 AIDL 接口
在另一个应用程序或组件中,通过 AIDL 接口调用服务中的方法:
// MyClientActivity.java
package com.example.client;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.Bundle;
import com.example.IMyInterface;
public class MyClientActivity extends Activity {
private IMyInterface mService;
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
mService = IMyInterface.Stub.asInterface(service);
try {
int result = mService.add(2, 3);
// 处理结果
} catch (RemoteException e) {
e.printStackTrace();
}
}
public void onServiceDisconnected(ComponentName className) {
mService = null;
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent();
intent.setClassName("com.example", "com.example.MyService");
bindService(intent, mConnection, BIND_AUTO_CREATE);
}
}
通过这个简单的例子,你可以了解如何使用 AIDL 在 Android 应用程序组件之间进行通信。记得在使用前测试和调试你的代码,确保一切正常运行。

9336

被折叠的 条评论
为什么被折叠?



