Android系统中,各应用程序都运行在自己的进程里,进程之间一般无法直接进行数据交换。为了实现这种跨进程通信(interprocess communication, IPC),Android提供了AIDL(Android Interface Definition Language,android接口定义语言) Service。

要使用AIDL进行通信,需要以下步骤:

服务端

1.定义AIDL接口。通常在该接口中定义业务方法。该接口文件以.aidl结尾。

2.使用platform-tools/aidl.exe工具将AIDL接口文件生成Stub。该Stub继承自android.os.Binder类。在ADT开发环境下会自动生成Stub在gen目录下。

3.自定义一个Binder类,继承自Stub,并且实现在AIDL接口中定义的业务方法。

4.定义一个service,在 onBind(Intent intent)方法中返回自定义的Binder对象。

5.在Androidmanifest.xml中注册该service。

注:AIDL接口文件中用到的数据类型,除了基本类型、String、List、Map以及CharSequence外,其他类型都需要导包,即使在同一包下。

客户端

1.将服务端的AIDL等相关文件拷贝到客户端。

2.绑定服务,并且在ServiceConnection.onServiceConnected(ComponentName name, IBinder service)方法中调用Stub.asInterface(service)方法返回一个AIDL接口,通过该接口就可以调用业务方法了。


下面通过一个例子来说明如何使用AIDL实现跨进程通信。

服务端

定义AIDL接口文件UserService.aidl:

package com.zzj.ui2.serviceAidlDemo;

import com.zzj.ui2.serviceAidlDemo.User;
import com.zzj.ui2.serviceAidlDemo.Book;

interface UserService{
	List<Book> getBooks(in User user);
}

Adt环境下会在gen目录下自动生成接口UserService.java,该接口包含一个Stub内部类:

/*
 * This file is auto-generated.  DO NOT MODIFY.
 * Original file: E:\\androidWorkspace\\zzj-android-demo2\\src\\com\\zzj\\ui2\\serviceAidlDemo\\UserService.aidl
 */
package com.zzj.ui2.serviceAidlDemo;
public interface UserService extends android.os.IInterface
{
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements com.zzj.ui2.serviceAidlDemo.UserService
{
private static final java.lang.String DESCRIPTOR = "com.zzj.ui2.serviceAidlDemo.UserService";
/** Construct the stub at attach it to the interface. */
public Stub()
{
this.attachInterface(this, DESCRIPTOR);
}
/**
 * Cast an IBinder object into an com.zzj.ui2.serviceAidlDemo.UserService interface,
 * generating a proxy if needed.
 */
public static com.zzj.ui2.serviceAidlDemo.UserService asInterface(android.os.IBinder obj)
{
if ((obj==null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof com.zzj.ui2.serviceAidlDemo.UserService))) {
return ((com.zzj.ui2.serviceAidlDemo.UserService)iin);
}
return new com.zzj.ui2.serviceAidlDemo.UserService.Stub.Proxy(obj);
}
@Override public android.os.IBinder asBinder()
{
return this;
}
@Override public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException
{
switch (code)
{
case INTERFACE_TRANSACTION:
{
reply.writeString(DESCRIPTOR);
return true;
}
case TRANSACTION_getBooks:
{
data.enforceInterface(DESCRIPTOR);
com.zzj.ui2.serviceAidlDemo.User _arg0;
if ((0!=data.readInt())) {
_arg0 = com.zzj.ui2.serviceAidlDemo.User.CREATOR.createFromParcel(data);
}
else {
_arg0 = null;
}
java.util.List<com.zzj.ui2.serviceAidlDemo.Book> _result = this.getBooks(_arg0);
reply.writeNoException();
reply.writeTypedList(_result);
return true;
}
}
return super.onTransact(code, data, reply, flags);
}
private static class Proxy implements com.zzj.ui2.serviceAidlDemo.UserService
{
private android.os.IBinder mRemote;
Proxy(android.os.IBinder remote)
{
mRemote = remote;
}
@Override public android.os.IBinder asBinder()
{
return mRemote;
}
public java.lang.String getInterfaceDescriptor()
{
return DESCRIPTOR;
}
@Override public java.util.List<com.zzj.ui2.serviceAidlDemo.Book> getBooks(com.zzj.ui2.serviceAidlDemo.User user) throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
java.util.List<com.zzj.ui2.serviceAidlDemo.Book> _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
if ((user!=null)) {
_data.writeInt(1);
user.writeToParcel(_data, 0);
}
else {
_data.writeInt(0);
}
mRemote.transact(Stub.TRANSACTION_getBooks, _data, _reply, 0);
_reply.readException();
_result = _reply.createTypedArrayList(com.zzj.ui2.serviceAidlDemo.Book.CREATOR);
}
finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
}
static final int TRANSACTION_getBooks = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
}
public java.util.List<com.zzj.ui2.serviceAidlDemo.Book> getBooks(com.zzj.ui2.serviceAidlDemo.User user) throws android.os.RemoteException;
}

Book类:

package com.zzj.ui2.serviceAidlDemo;

import android.os.Parcel;
import android.os.Parcelable;

public class Book implements Parcelable {
	public static final Creator<Book> CREATOR = new Creator<Book>() {

		@Override
		public Book[] newArray(int size) {
			return new Book[size];
		}

		@Override
		public Book createFromParcel(Parcel source) {
			Book book = new Book();
			book.setName(source.readString());
			book.setPrice(source.readDouble());
			return book;
		}
	};
	private String name;
	private double price;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeString(name);
		dest.writeDouble(price);
	}

}

User类:

package com.zzj.ui2.serviceAidlDemo;

import android.os.Parcel;
import android.os.Parcelable;

public class User implements Parcelable {
	public static final Creator<User> CREATOR = new Creator<User>() {

		@Override
		public User createFromParcel(Parcel source) {
			User user = new User();
			user.setUsername(source.readString());
			user.setPassword(source.readString());
			return user;
		}

		@Override
		public User[] newArray(int size) {
			return new User[size];
		}
	};

	private String username;
	private String password;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeString(username);
		dest.writeString(password);
	}

}

自定义的类型如果需要被AIDL接口引用,必须在相应的AIDL文件中声明。

Book.aidl:

package com.zzj.ui2.serviceAidlDemo;
parcelable Book;

User.aidl:

package com.zzj.ui2.serviceAidlDemo;
parcelable User;

自定义的类型需要实现Parcelable接,服务端通过writeToParcel(Parcel dest, int flags)方法实现序列化;然后定义了一个静态的Creator成员变量,客户端通过createFromParcel(Parcel source)方法实现反序列化。

定义一个service:

package com.zzj.ui2.serviceAidlDemo;

import java.util.ArrayList;
import java.util.List;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;

import com.zzj.ui2.serviceAidlDemo.UserService.Stub;

public class AidlService extends Service {

	private IBinder binder = new Mybinder();

	@Override
	public IBinder onBind(Intent intent) {
		return binder;
	}

	private class Mybinder extends Stub {

		@Override
		public List<Book> getBooks(User user) throws RemoteException {
			List<Book> books = new ArrayList<Book>();
			if (user == null) {
				return books;
			}
			if ("001".equals(user.getUsername())
					&& "123".equals(user.getPassword())) {

				Book book = new Book();
				book.setName("时间简史");
				book.setPrice(30.5d);
				books.add(book);

				book = new Book();
				book.setName("失控");
				book.setPrice(50.5d);
				books.add(book);
			}
			return books;
		}
	}

}

在服务端的Androidmanifest.xml中注册该service:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.zzj.ui2"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <!-- 声明外部调用本应用service所需的权限 -->
    <permission android:name="com.zzj.AIDL_SERVICE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <!-- 1.com.zzj.ui2.serviceAidlDemo -->
        <service
            android:name="com.zzj.ui2.serviceAidlDemo.AidlService"
            android:permission="com.zzj.AIDL_SERVICE" >
            <intent-filter>
                <action android:name="com.zzj.ui2.AIDL_SERVICE" />
            </intent-filter>
        </service>
    </application>

</manifest>

将服务端部署到设备上。至此服务端开发完毕。

客户端

将UserService.aidl、Book.java、User.java、Book.aidl、User.aidl等文件从服务端拷贝到客户端。注意:客户端的包名要与服务端的包名一致。

在Activity中绑定远程service:

package com.zzj.ui.serviceAidlDemo;

import java.util.List;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.zzj.ui.R;
import com.zzj.ui2.serviceAidlDemo.Book;
import com.zzj.ui2.serviceAidlDemo.User;
import com.zzj.ui2.serviceAidlDemo.UserService;
import com.zzj.ui2.serviceAidlDemo.UserService.Stub;

public class MainActivity extends Activity {
	private UserService userService;
	private AidlServiceConnection serviceConnection;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.serviceaidl_activity);

		// 绑定服务
		Intent service = new Intent();
		service.setAction("com.zzj.ui2.AIDL_SERVICE");
		serviceConnection = new AidlServiceConnection();
		bindService(service, serviceConnection, Context.BIND_AUTO_CREATE);
	}

	public void getBooks(View v) {
		User user = new User();
		user.setUsername(((EditText) findViewById(R.id.username_edt)).getText()
				.toString());
		user.setPassword(((EditText) findViewById(R.id.password_edt)).getText()
				.toString());
		try {
			List<Book> books = userService.getBooks(user);
			if (books == null || books.isEmpty()) {
				Toast.makeText(this, "该用户没有订书或者用户名或密码错误!", Toast.LENGTH_LONG)
						.show();
			} else {
				StringBuilder sb = new StringBuilder();
				sb.append("查询成功!\r\n");
				for (Book book : books) {
					sb.append(book.getName() + " --> " + book.getPrice()
							+ "\r\n");
				}
				Toast.makeText(this, sb.toString(), Toast.LENGTH_LONG).show();
			}
		} catch (RemoteException e) {
			e.printStackTrace();
			Toast.makeText(this, "查询出错!", Toast.LENGTH_LONG).show();
		}
	}

	private class AidlServiceConnection implements ServiceConnection {

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			userService = Stub.asInterface(service);
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			userService = null;
		}

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		// 解绑服务
		unbindService(serviceConnection);
	}
}

Activity布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/username_edt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="帐号" >
    </EditText>

    <EditText
        android:id="@+id/password_edt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:ems="10"
        android:hint="密码" />

    <Button
        android:id="@+id/getBooks_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="getBooks"
        android:text="getbooks" />

</LinearLayout>

在Androidmanifest.xml中申请调用service的权限:

<!-- 调用aidl service权限 -->
    <uses-permission android:name="com.zzj.AIDL_SERVICE" />



测试效果:

android 跨进程 ContentProvider android 跨进程启动app_User