**寫作原因:跨進(jìn)程通信的實(shí)現(xiàn)和理解是Android進(jìn)階中重要的一環(huán)。下面博主分享IPC一些相關(guān)知識(shí)咪橙、操作及自己在學(xué)習(xí)IPC過程中的一些理解夕膀。這一章是對AIDL的基本使用的介紹虚倒,由于博主也是AIDL初學(xué)者,所以一些地方闡述可能不準(zhǔn)確产舞,如有發(fā)現(xiàn)希望指正魂奥。關(guān)于使用Messenger通信參閱:Android IPC機(jī)制(二)——利用Messenger實(shí)現(xiàn)跨進(jìn)程通信
**
AIDL基本介紹
關(guān)于AIDL,先看看Google官方文檔對它的定義:AIDL是接口定義語言庞瘸,它允許你定義程序接口實(shí)現(xiàn)跨進(jìn)程的客戶端與服務(wù)端的通信捧弃。在Android中赠叼,一個(gè)進(jìn)程通常不能訪問另一個(gè)進(jìn)程的內(nèi)存(這是IPC出現(xiàn)的原因)擦囊,也就是說,進(jìn)程之間要想通信嘴办,就需要將想要共享的對象分解成操作系統(tǒng)能夠理解的原始元素通過底層來實(shí)現(xiàn)通信瞬场。這個(gè)過程是乏味無趣的,所以Android幫我們通過AIDL解決了這個(gè)問題涧郊。(個(gè)人翻譯)此外贯被,官方文檔有說明,只有在不同的客戶端與服務(wù)端通信妆艘,服務(wù)端并發(fā)處理是才有必要使用這種方式彤灶,否則選擇Messenger可能更合適。相比Messenger批旺,AIDL有這些優(yōu)勢:一幌陕、跨進(jìn)程調(diào)用服務(wù)端的方法;二汽煮、服務(wù)端并發(fā)處理數(shù)據(jù)時(shí)搏熄。上述兩種情況使用AIDL更加合適。
基本用法
下面開始掌握AIDL的使用方法暇赤,先概述一下基本用法:
1.服務(wù)端創(chuàng)建一個(gè)Service監(jiān)聽處理請求心例,創(chuàng)建一個(gè)AIDL文件將暴露給客戶端的接口在AIDL文件中聲明,然后在Service中實(shí)現(xiàn)AIDL接口鞋囊。
2.客戶端綁定Service獲得Binder對象轉(zhuǎn)化成AIDL接口所屬的類型止后,調(diào)用接口中的方法。
具體實(shí)現(xiàn)
具體實(shí)現(xiàn)見下面(下面例子是官方舉例的升級(jí)版溜腐,可以學(xué)習(xí)一下):
本例中共有上個(gè)文件坯门,MainActivity.java(客戶端)、RemoteService.java(服務(wù)端)逗扒、Command.java(實(shí)體類)古戴、IRemoteService.aidl(AIDL文件)、Command.aidl(AIDL文件)矩肩。
1.編寫AIDL暴露接口
AIDL文件中只支持六種數(shù)據(jù)類型:基本數(shù)據(jù)類型现恼、ArrayList肃续、HashMap、Parcelable和AIDL叉袍。本例中我使用基本數(shù)據(jù)類型和Parcelable來闡述始锚。
由于下面我要使用Parcelable序列化的對象Command,所以先寫好Command類:
public class Command implements Parcelable{
String name;
int id;
public static final Parcelable.Creator<Command> CREATOR = new Parcelable.Creator<Command>(){
@Override
public Command createFromParcel(Parcel source) {
//實(shí)現(xiàn)從Parcel容器讀取傳遞數(shù)據(jù)值喳逛,封裝成Parcelable對象返回邏輯層
return new Command(source);
}
@Override
public Command[] newArray(int size) {
//創(chuàng)建一個(gè)類型為T長度為size的數(shù)組瞧捌,僅一句話即可
return new Command[size];
}
};
private Command(){
}
private Command(Parcel source){
name = source.readString();
id = source.readInt();
}
@Override
public String toString() {
return id+name;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
//將對象映射成Parcelable對象,注意與讀取的順序相同
dest.writeString(name);
dest.writeInt(id);
}
}
關(guān)于Parcelable序列化润文,在這里就不多加闡述了姐呐,不然這篇文章不知道要多長……序列化的基本方法見上面。官方推薦使用Parcelable是有原因的典蝌,Parcelable效率相對高曙砂,雖然實(shí)現(xiàn)起來比較麻煩。上面類寫好之后新建一個(gè)Command.aidl文件骏掀,內(nèi)容如下:
package com.example.vincebarry.googleaidl;
parcelable Command;
下面實(shí)現(xiàn)一下IRemoteService.aidl:
package com.example.vincebarry.googleaidl;
// Declare any non-default types here with import statements
import com.example.vincebarry.googleaidl.Command;
interface IRemoteService {
int getPid();
Command getCommand();
void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
double aDouble, String aString);
}
這里有兩個(gè)部分要注意:其一是開放的方法接口要準(zhǔn)確鸠澈,其二是把序列化的Command類導(dǎo)入該aidl文件之中。
這樣截驮,我們就把所需的aidl文件寫完了笑陈,總結(jié)一下:
首先是一個(gè)業(yè)務(wù)接口aidl文件,該文件包含所有的Service暴露給客戶端的方法葵袭。此外如果在該文件中使用序列化對象必須將其導(dǎo)入涵妥;
序列化對象實(shí)現(xiàn)Parcelable接口,實(shí)現(xiàn)序列化和反序列化功能眶熬,并且將其注冊到aidl文件中妹笆,注冊過程見上面;
2.Service的實(shí)現(xiàn)
這里主要是將接口中的方法實(shí)現(xiàn)并且打包到(該詞可能不準(zhǔn)確)IBinder對象中將其發(fā)送到與該Service綁定的客戶端去娜氏。主要實(shí)現(xiàn)了返回當(dāng)前線程的id和將Service中的Command對象返回的邏輯拳缠,具體代碼如下:
public class RemoteService extends Service {
private Command myCommand;
@Override
public IBinder onBind(Intent intent) {
myCommand = new Command();
myCommand.setId(10);
myCommand.setName("Hello");
Toast.makeText(getApplicationContext(),"Remote Service bound",Toast.LENGTH_SHORT).show();
return new IRemoteService.Stub(){
@Override
public int getPid() throws RemoteException {
return Process.myPid();
}
@Override
public Command getCommand() throws RemoteException {
return myCommand;
}
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
}
};
}
}
3.客戶端的實(shí)現(xiàn)
客戶端方面主要是將Activity與Service綁定,然后將接口存根取出轉(zhuǎn)化為接口贸弥,利用該接口來調(diào)用服務(wù)中的方法窟坐。具體代碼如下:
ublic class MainActivity extends AppCompatActivity {
private static final String SERVICE_ACTION = "com.example.vincebarry.googleaidl.RemoteService";
private IRemoteService iRemoteService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(MainActivity.this,RemoteService.class);
}
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
iRemoteService = IRemoteService.Stub.asInterface(service);
int i = -1;
String command = null;
try {
i = iRemoteService.getPid();
command = iRemoteService.getCommand().getName()+iRemoteService.getCommand().id;
} catch (RemoteException e) {
e.printStackTrace();
}
if(i != -1){
Log.i("Pid","Remote Service:"+i);
Log.i("Pid","Remote Service:"+command);
}else{
Log.i("Pid","Remote Service:"+"null");
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
iRemoteService = null;
}
};
}
總結(jié)
實(shí)現(xiàn)下來發(fā)現(xiàn)其實(shí)理解上難度并不大,只是稍顯麻煩绵疲,只要前面兩章理解好哲鸳,對于AIDL的通信還是可以較好掌握的,關(guān)鍵還是要多實(shí)踐盔憨。關(guān)于多線程并發(fā)的情況本文沒有講到徙菠,由于博主對于并發(fā)編程的掌握局限性的原因就暫時(shí)不實(shí)現(xiàn)并發(fā)情況下的AIDL通信情況,以后對并發(fā)理解加深后再作討論郁岩。