一、编写服务端代码
1、新建一个Book类,实现Parcelable
package com.licheng.android.weextest;
import android.os.Parcel;
import android.os.Parcelable;
/**
* Created by licheng on 15/11/16.
*/
public class Book implements Parcelable {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
private String name;
private int price;
public Book(){}
public Book(Parcel in) {
name = in.readString();
price = in.readInt();
}
public static final Creator CREATOR = new Creator() {
@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.writeString(name);
dest.writeInt(price);
}
/**
* 参数是一个Parcel,用它来存储与传输数据
* @param dest
*/
public void readFromParcel(Parcel dest) {
//注意,此处的读值顺序应当是和writeToParcel()方法中一致的
name = dest.readString();
price = dest.readInt();
}
//方便打印数据
@Override
public String toString() {
return "name : " + name + " , price : " + price;
}
}
新建aidl文件 通过如图所示步骤新建两个aidl文件,分别是Book.aidl和BookManager.aidl
Book.aidl文件内容,其中parcelable Book,即定义Book类的Parcelbale对象,供其他aidl文件使用。因为Parcelbale数据类型不是aidl默认支持的,所以这里需要定义。
// Book.aidl
package com.licheng.android.weextest;
// Declare any non-default types here with import statements
parcelable Book;
// BookManager.aidl
package com.licheng.android.weextest;
import com.licheng.android.weextest.Book;
// Declare any non-default types here with import statements
interface BookManager {
List getBooks();
void addBook(in Book book);
}
Book类的包名要和两个aidl文件的包名保持一致。
在build.gradle中添加下面代码,用来指定aidl文件夹位置。
sourceSets {
main {
java.srcDirs = ['src/main/java', 'src/main/aidl']
}
}
2、编写服务端用来通信的service类,这里我命名为AIDLService
package com.licheng.android.weextest;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
/**
* Created by licheng on 15/11/16.
*/
public class AIDLService extends Service {
public final String TAG = this.getClass().getSimpleName();
private List mBooks = new ArrayList<>();
private final BookManager.Stub mBookManager = new BookManager.Stub() {
@Override
public List getBooks() throws RemoteException {
synchronized (this){
Log.e(TAG, "invoking getBooks() method, new the list is : " + mBooks.toString());
if(null != mBooks){
return mBooks;
}
}
return null;
}
@Override
public void addBook(Book book) throws RemoteException {
synchronized (this){
if(null == mBooks){
mBooks = new ArrayList<>();
}
if(null == book){
Log.e(TAG, "Book is null in In");
book = new Book();
}
book.setPrice(1234);
if(!mBooks.contains(book)) {
mBooks.add(book);
}
//打印mBooks列表,观察客户端传过来的值
Log.e(TAG, "invoking addBooks() method , now the list is : " + mBooks.toString());
}
}
};
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public boolean onUnbind(Intent intent) {
return super.onUnbind(intent);
}
@Override
public void onCreate() {
super.onCreate();
Book book = new Book();
book.setName("Android开发艺术探索");
book.setPrice(79);
mBooks.add(book);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.e(TAG, String.format("on bind, intent = %s", intent.toString()));
return mBookManager;
}
}
二、编写客户端代码
1、新建一个客户端工程,把之前在另一个服务端工程写好的aidl文件夹整体复制到客户端工程的src/main目录下,我们还需要把服务端写好的Book类复制到aidl文件下以包名命名的文件里,Book类的文件位置需要和两个aidl文件处于同一级(因为之前写服务端BookManager.aidl文件时,我们定义Book的地址是com.licheng.android.weextest.Book,到客户端Book类的引用地址需要和服务端中的Book类的地址保持一致)。
同样,在build.gradle文件中添加
sourceSets {
main {
java.srcDirs = ['src/main/java', 'src/main/aidl']
}
}
package com.licheng.android.weextesttwo;
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.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.licheng.android.weextest.Book;
import com.licheng.android.weextest.BookManager;
import java.util.List;
/**
* Created by licheng on 15/11/16.
*/
public class AIDLActivity extends AppCompatActivity {
private BookManager mBookManger = null;
private boolean mBound = false;
private List mBooks;
private Button btnBindService;
@Override
protected void onStart() {
super.onStart();
if(!mBound){
attemptToBindService();
}
}
@Override
protected void onStop() {
super.onStop();
if(mBound){
unbindService(connection);
mBound = false;
}
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnBindService = (Button) findViewById(R.id.btnBindService);
btnBindService.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(!mBound){
attemptToBindService();
Toast.makeText(AIDLActivity.this, "当前与服务端处于未连接状态,正在尝试重连,请稍后尝试", Toast.LENGTH_SHORT).show();
return;
}
if(null == mBookManger){
return;
}
Book book = new Book();
book.setName("APP研发录In");
book.setPrice(30);
try {
mBookManger.addBook(book);
Log.e(getLocalClassName(), book.toString());
} catch (RemoteException e) {
e.printStackTrace();
}
}
});
}
private void attemptToBindService() {
Intent intent = new Intent();
intent.setAction("com.licheng.android.weextest.Book");
intent.setPackage("com.licheng.android.weextest");
bindService(intent, connection, BIND_AUTO_CREATE);
}
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
Log.e(getLocalClassName(), "service connected");
mBookManger = BookManager.Stub.asInterface(iBinder);
mBound = true;
if(null != mBookManger){
try {
mBooks = mBookManger.getBooks();
Log.e(getLocalClassName(), mBooks.toString());
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
Log.e(getLocalClassName(), "service disconnected");
mBound = false;
}
};
}
注意,客户端跳转服务端的Service中的Action设置的是com.licheng.android.weextest.Book,该action即在服务端manifests文件中定义service时设置的action,这样,客户端才能正确找到服务。
三、测试客户端和服务端的通信
测试开始前,我们需要启动服务端的应用程序,以确保service处于运行状态。然后点击客户端的"添加书本"按钮。
启动AIDLActivity时,如果服务端service处于运行状态,则客户端一启动就会连接上service。获取到了服务端的信息,打印信息如下:
当我们点击客户端“添加书本”按钮时,则会向服务端添加一本书的信息。服务端就会收到客户端传来的数据,打印信息如下:
原文参考自: http://blog.csdn.net/luoyanglizi/article/details/51980630