本篇文章主要是讲述AIDL是如何使用的,而不会太具体地介绍一些代码上面的细节。具体的代码细节能够参考IPC机制之Binder机制这篇文章。咱们设定一个情景,客户端向服务端添加书籍并获取当前书籍列表的信息,咱们来看一下应该怎么实现。java
使用AIDL,也就是在Binder的基础上面进行进程间的通讯。既然是双端通讯,必须有一个客户端,一个服务端。这里咱们建立一个服务端:android
public class BookService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
复制代码
服务端须要返回一个Binder对象给客户端,咱们须要建立一个Binder对象:bash
private Binder binder = new IBookManager.Stub() {
@Override
public void addBook(Book book) throws RemoteException {
if (book != null) {
mBookList.add(book);
}
}
@Override
public List<Book> getBookList() throws RemoteException {
return mBookList;
}
};
复制代码
建立Binder对象须要一个Stub类继承了Binder类,而这里的IBookManager须要使用AIDL文件生成。app
// IBookManager.aidl
package com.example.runningh.myapplication.aidl;
// Declare any non-default types here with import statements
import com.example.runningh.myapplication.aidl.Book;
interface IBookManager {
void addBook(in Book book);
List<Book> getBookList();
}
复制代码
这里须要注意的是在AIDL中除了基本数据类型,其余类型的参数必须标明方向:in, out或者intout,in表示输入型参数,out表示输出型参数,inout表示输入输出型参数,咱们须要根据实际须要去指定参数类型,不能一律使用out或者inout,由于这在底层实现是有开销的。并且在AIDL接口中只支持方法,不支持声明静态常量,这一点区别于传统的接口。ide
// Book.aidl
package com.example.runningh.myapplication.aidl;
// Declare any non-default types here with import statements
import com.example.runningh.myapplication.aidl.Book;
parcelable Book;
复制代码
建立Book类并实现Parcelable接口。ui
package com.example.runningh.myapplication.aidl;
import android.os.Parcel;
import android.os.Parcelable;
/**
* Created by RunningH on 2017/12/10.
*/
public class Book implements Parcelable {
public int bookId;
public String bookName;
public Book(int bookId, String bookName) {
this.bookId = bookId;
this.bookName = bookName;
}
protected Book(Parcel in) {
bookId = in.readInt();
bookName = in.readString();
}
public static final Creator<Book> CREATOR = new Creator<Book>() {
@Override
public Book createFromParcel(Parcel in) {
return new Book(in);
}
@Override
public Book[] newArray(int size) {
return new Book[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(bookId);
dest.writeString(bookName);
}
}
复制代码
当aidl文件建立完毕后,经过Build—>make module project生成IBookManager.java文件。this
客户端咱们使用BookManagerActivity。这里使用的是bindService的方式和服务端进行绑定起来。spa
package com.example.runningh.myapplication.aidl;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.util.Log;
import com.example.runningh.myapplication.R;
import java.util.List;
/**
* Created by RunningH on 2017/12/10.
*/
public class BookManagerActivity extends Activity {
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
IBookManager iBookManager = IBookManager.Stub.asInterface(service);
try {
List<Book> bookList = iBookManager.getBookList();
Log.i("ABC", "query book list:" + bookList.toString());
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this, BookService.class);
bindService(intent, mConnection, BIND_AUTO_CREATE);
}
}
复制代码
最后咱们须要在Manifest中注册上述咱们建立的服务端,并将其运行在remote进程中。3d
<service
android:name=".aidl.BookService"
android:process=".remote"/>
复制代码
最后在Logcat中看到结果以下: query book list:[com.example.runningh.myapplication.aidl.Book@267a8b7, com.example.runningh.myapplication.aidl.Book@14c824]code