AIDL 文件可以分为两类。一类用来声明实现了 Parcelable 接口的数据类型以供其他 AIDL 文件使用那些非默认支持的数据类型。还有一类是用来定义接口方法声明要暴露哪些接口给客户端调用。在 AIDL 文件中需要明确标明引用到的数据类型所在的包名即使两个文件处在同个包名下。
默认情况下AIDL 支持下列数据类型
-
八种基本数据类型byte、char、short、int、long、float、double、boolean StringCharSequence
-
List类型。List承载的数据必须是AIDL支持的类型或者是其它声明的AIDL对象
-
Map类型。Map承载的数据必须是AIDL支持的类型或者是其它声明的AIDL对象
服务端目录结构与相关代码
- 创建Book数据类跨进程传递对象需将对象序列化处理
package com.example.aidl_server.aidl;import android.os.Parcel;import android.os.Parcelable;public class Book implements Parcelable { private String name; protected Book(Parcel in) { name in.readString(); } 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); } Override public String toString() { return "Book{" "name" name \ }; }}
- Book对应的AIDL文件代码内容
package com.example.aidl_server.aidl;parcelable Book;
注意这里Book.java与Book.aidl里package一定要一致不然后面build时会报错无法生成相关接口代码。
- 对客户端提供接口方法的aidl代码此文件与Book.aidl在同一路径下
// IBookInterface.aidlpackage com.example.aidl_server.aidl;import com.example.aidl_server.aidl.Book;// Declare any non-default types here with import statementsinterface IBookInterface { void addBook(in Book book); List getBooks(); /** * Demonstrates some basic types that you can use as parameters * and return values in AIDL. */ void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString);}
创建完成后build一下代码build成功的话可以在此路径下看见生成的java代码
- 创建服务BookService
package com.example.aidl_server;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.os.RemoteException;import android.util.Log;import androidx.annotation.Nullable;import com.example.aidl_server.aidl.Book;import com.example.aidl_server.aidl.IBookInterface;import java.util.ArrayList;import java.util.List;public class BookService extends Service { private IBookInterface.Stub binder new IBookInterface.Stub() { private List books new ArrayList(); Override public void addBook(Book book) throws RemoteException { if (book ! null) { books.add(book); Log.e("BookService", "添加书籍成功 " book.toString()); } } Override public List getBooks() throws RemoteException { Log.e("BookService", "获取书籍数目为 " books.size()); return books; } Override public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException { } }; Nullable Override public IBinder onBind(Intent intent) { return binder; }}
- 服务端清单文件内注册此服务
服务端准备工作完成接下来看一下客户端如何操作 1. 先将服务端aidl文件夹完整的复制过来包名要与服务端保持一致 2. 将服务端Book文件复制到客户端包名要与服务端一致 3. build代码 package com.example.aidl_client;import androidx.appcompat.app.AppCompatActivity;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.TextView;import android.widget.Toast;import com.example.aidl_server.aidl.Book;import com.example.aidl_server.aidl.IBookInterface;import java.util.List;public class ClientActivity extends AppCompatActivity { private IBookInterface iBookInterface; private ServiceConnection connection new ServiceConnection() { Override public void onServiceConnected(ComponentName name, IBinder service) { iBookInterface IBookInterface.Stub.asInterface(service); } Override public void onServiceDisconnected(ComponentName name) { } }; Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_client); // 绑定服务 Intent intent new Intent(); intent.setAction("com.example.aidl_server.jiao"); intent.setPackage("com.example.aidl_server"); bindService(intent, connection, Context.BIND_AUTO_CREATE); TextView bookShow findViewById(R.id.show_book_name); // 点击添加书籍 findViewById(R.id.add_book).setOnClickListener(v -> { long millis System.currentTimeMillis(); Book book new Book("书籍" millis); try { iBookInterface.addBook(book); Toast .makeText(getApplicationContext(), "添加成功", Toast.LENGTH_LONG) .show(); } catch (Exception e) { e.printStackTrace(); Toast .makeText(getApplicationContext(), "添加失败", Toast.LENGTH_LONG) .show(); } }); // 点击显示书籍 findViewById(R.id.get_book).setOnClickListener(v -> { try { List books iBookInterface.getBooks(); if (books ! null) { if (books.size() 0) { bookShow.setText("书记数目为0"); } else { bookShow.setText(books.toString()); } } } catch (Exception e) { e.printStackTrace(); } }); } Override protected void onDestroy() { super.onDestroy(); unbindService(connection); }} 客户端布局相关代码客户端连接服务并且进行数据交互
客户端目录结果如下图
【文章原创作者:欧洲服务器 http://www.558idc.com/helan.html 复制请保留原URL】