目录
基本概念:
Android Interface Definition Language,即Android接口定义语言。Android系统中的进程之间不能共享内存,因此,需要提供一些机制在不同进程之间进行数据通信。为了使其他的应用程序也可以访问本应用程序提供的服务,Android系统采用了远程过程调用(Remote Procedure Call,RPC)方式来实现。
AIDL支持的数据类型
-
java基本数据类型、String和CharSequence
-
List和Map(每个数据元素都被aidl支持)
-
实现了Parcelable的实例
-
其他实现了AIDL的实例
基本实现
-
根据需求实现需要操作的实体类,实现 Parcelable 接口
public class Book implements Parcelable { private int bookId; private String name; private String author; protected Book(Parcel in) { bookId = in.readInt(); name = in.readString(); author = 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( name ); dest.writeString( author ); } }
-
声明AIDL文件
在 main 文件夹下新建 aidl 文件夹,使用的包名要和 java 文件夹的包名一致
注意:在aidl中使用到的自定义实现Parcelable 的类,也需要对应的aidl声明,包名和实体类的包名一致
非基本类型的数据需要导入,比如上面的 Book,需要导入它的全路径。 这里的 Book我理解的是 Book.aidl,然后通过 Book.aidl 又找到真正的实体 Book类。 方法参数中,除了基本数据类型,其他类型的参数都需要标上方向类型 。in(输入),out(输出),inout(输入输出)。
// Book.aidl package com.example.txl.tool.inter.process.communication; // Declare any non-default types here with import statements parcelable Book;
// IBookManager.aidl package com.example.txl.tool.inter.process.communication; // Declare any non-default types here with import statements import com.example.txl.tool.inter.process.communication.Book; interface IBookManager { /** * Demonstrates some basic types that you can use as parameters * and return values in AIDL. */ List<Book> getBookList(); void addBook(in Book book); }
-
实现服务端的代码
package com.example.txl.tool.inter.process.communication; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.os.RemoteException; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; public class BookManagerService extends Service { private static final String TAG = "BookManagerService"; private CopyOnWriteArrayList<Book> mBookList = new CopyOnWriteArrayList<>( ); private Binder mBinder = new IBookManager.Stub() { @Override public List<Book> getBookList() throws RemoteException { return mBookList; } @Override public void addBook(Book book) throws RemoteException { mBookList.add( book ); } }; public BookManagerService() { } @Override public void onCreate() { super.onCreate(); mBookList.add( new Book( 1,"Android","小王" ) ); mBookList.add( new Book( 2,"IOS" ,"小张") ); } @Override public IBinder onBind(Intent intent) { return mBinder; } }
<service android:name=".inter.process.communication.BookManagerService" android:enabled="true" android:exported="true" android:process=":remote" > <intent-filter > <action android:name="com.txl.aidl.demo"/> </intent-filter> </service>
实现客户端代码:
public class AidlDemoActivity extends AppCompatActivity { private TextView tvGetBookList, tvAddBook; IBookManager bookManager; ServiceConnection serviceConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) {