IPC(Inter-Process Communication) 进程间通信,是指两个不同进程之间数据交换的过程。
AIDL:Android Interface Definition Language,即Android接口定义语言。
其原理和用法,网上也是各种介绍,初学本着用过则为熟悉AIDL的心理。自己写一遍,加深印象和理解。
首先,梳理一下流程:
1、编写aidl文件;
2、生成对应的java文件;
3、调用接口实现跨进程通信。
因为需要服务端和客户端共用aidl
文件,所以最好单独建一个包,适合拷贝到客户端。
服务端APP编写
1、创建Boo.java,该对象作为传输。实现Parcelable序列化,通过序列化在进程间传递对象。
package com.example.aidl;
import android.os.Parcel;
import android.os.Parcelable;
/**
* @author RB
* @date 2018/5/30 15:57
*/
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 parcel, int i) {
parcel.writeInt(bookId);
parcel.writeString(bookName);
}
@Override
public String toString() {
return "Book{" +
"bookId=" + bookId +
", bookName='" + bookName + '\'' +
'}';
}
}
2、创建Book.aidl
文件,首先创建一个AIDL包,与java同路径,并手动添加文件。
// Book.aidl
package com.example.aidl;
parcelable Book;
3、创建IBookManager.aidl
文件,接口文件,面向客户端调用
// IBookManager.aidl
package com.example.aidl;
// Declare any non-default types here with import statements
import com.example.aidl.Book;
interface IBookManager {
List<Book> getBookList();
void addBook(in Book book);
}
写完之后clean
一下工程,之后会在gen
目录下生成对应的java文件。
这样方法接口基本就写好了,后面就是调用的问题了。
4、创建Service
类,用于相互通信。
package com.example.bookservice;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import com.example.aidl.Book;
import com.example.aidl.IBookManager;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
/**
* @author RB
* @date 2018/5/30 17:28
*/
public class BookService extends Service {
/**
* 支持线程同步,因为其存在多个客户端同时连接的情况
*/
private CopyOnWriteArrayList<Book> list = new CopyOnWriteArrayList<>();
/**
* 构造 aidl中声明的接口的Stub对象,并实现所声明的方法
*/
private Binder mBinder = new IBookManager.Stub() {
@Override
public List<Book> getBookList() throws RemoteException {
return list;
}
@Override
public void addBook(Book book) throws RemoteException {
list.add(book);
Log.i("aidl", "服务端添加了一本书"+book.toString());
}
};
@Override
public void onCreate() {
super.onCreate();
//加点书
list.add(new Book(1, "java"));
list.add(new Book(2, "android"));
Log.i("aidl", "服务端第一次自动添加了一本书");
}
@Override
public IBinder onBind(Intent intent) {
// 返回给客户端的Binder对象
return mBinder;
}
}
Service
中,主要干了两件事情:
- 实现aidl文件中的接口的Stub对象。并实现方法。
- 将
Binder
对象通过onBinder
返回给客户端。
记得在Androidmanifest.xml中添加:
<service
android:name="com.example.bookservice.BookService"
android:exported="true">
</service>
开始编写客户端APP
将aidl包拷贝到main
编写Activity
package com.example.bookclient;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import com.example.aidl.Book;
import com.example.aidl.IBookManager;
public class MainActivity extends AppCompatActivity {
/**
* 接口对象
*/
private IBookManager mService;
//标志当前与服务端连接状况的布尔值,false为未连接,true为连接中
private boolean mBound = false;
/**
* 绑定服务的回调
*/
private ServiceConnection conn = new ServiceConnection(){
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// 获取到书籍管理的对象
mService = IBookManager.Stub.asInterface(service);
Log.i("aidl", "连接到服务端,获取IBookManager的对象");
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
attemptToBindService();
}
/**
* 尝试与服务端建立连接
*/
private void attemptToBindService() {
Intent intent = new Intent();
// intent.setAction("com.example.aidl");
// intent.setPackage("com.example.bookservice");
intent.setComponent(new ComponentName("com.example.bookservice", "com.example.bookservice.BookService"));
bindService(intent, conn, Context.BIND_AUTO_CREATE);
}
/**
* 获取服务端书籍列表
* @param view
*/
public void getBookList(View view){
try {
Log.i("aidl","客户端查询书籍"+mService.getBookList().toString());
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 添加书籍
*/
public void add(View view){
try {
// 调用服务端添加书籍
if (!mBound) {
attemptToBindService();
mService.addBook(new Book(3, "ios"));
}
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
两个button:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="getBookList"
android:text="getBookList"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="add"
android:text="add"
/>
客户端的代码主要效果:
- 绑定服务,监听回调。
- 将回调中的
IBinder service
通过IBookManager.Stub.asInterface()
转化为接口对象。 - 调用接口对象的方法。
先启动serviceAPP,然后运行clientAPP。
效果图:
毕竟是参考网上的例子进行学习,这个Book太经典了,没有什么好改进的,就照抄了demo。
想了解更多关于IPC通信机制原理的可以看看以下博文:
https://blog.csdn.net/lisdye2/article/details/51775856
https://blog.csdn.net/luoyanglizi/article/details/51980630