Android AIDL

作者: 编程的猫 | 来源:发表于2020-02-27 18:07 被阅读0次

Android进程间的通信方式

  • Bundle
  • file
  • aidl
  • ContentProvider
  • Network


    image.png

AIDL用来做什么

AIDL是Android中IPC(Inter-Process Communication)方式中的一种,AIDL是Android Interface definition language的缩写,对于小白来说,AIDL的作用是让你可以在自己的APP里绑定一个其他APP的service,这样你的APP可以和其他APP交互。(可以跨进程读写内存数据)

下面简单记录下实践,明天详细写:

项目一:

image.png
public class Book implements Parcelable {
    private String bookName;

    public Book(String bookName) {
        this.bookName = bookName;
    }

    protected Book(Parcel in) {
        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];
        }
    };

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

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

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

    public void readFromParcel(Parcel in){
        this.bookName=in.readString();
    }

    @Override
    public String toString() {
        return "Book{" +
                "bookName='" + bookName + '\'' +
                '}';
    }
}

编写完aidl文件和Book类之后,Build ,Make Project后会生成相应的文件。在Make Project后可能会readFromParcel的错误,在Booklei中添加readFromParcel方法即可,如下图:


image.png
image.png
public class BookService extends Service {

    private final String TAG = BookService.class.getSimpleName();
    private List<Book> list;

    public BookService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        list = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            Book book = new Book("第" + i + "书");
            list.add(book);
        }
    }

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

    private BookManager.Stub manager = new BookManager.Stub() {
        @Override
        public List<Book> getBookList() throws RemoteException {
            Log.d(TAG, "getBookList");
            return list;
        }

        @Override
        public void addBook(Book book) throws RemoteException {
            Log
                    .d(TAG, "addBook");
            if (book != null) {
                list.add(book);
                Log.d(TAG, book.toString());
            }
        }
    };
}

项目二

image.png

在项目二activity中编写测试代码。运行两个项目安装两个应用,在客户端应用中绑定Service,拿到manager,调用geiBookList和addBook的方法。

public class MainActivity extends AppCompatActivity {

    private final String TAG = MainActivity.class.getSimpleName();
    private BookManager manager;

    private final String ACTION = "com.example.ikapplication.BookService";

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


    }

    public void bindBookService(View view) {
        Intent intent = new Intent(ACTION);
        //注意:Android5.0之后不支持隐式引动Service,必须指定包名
        intent.setPackage("com.example.ikapplication");
        bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
    }

    public void addBook(View view) {
        try {
            if (manager != null) {
                Book book = new Book("光谷崛起之路");
                manager.addBook(book);
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    public void getBookList(View view) {
        try {
            if (manager != null) {
                List<Book> bookList = manager.getBookList();
                Log.d(TAG, bookList.toString());
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.d(TAG, "onServiceConnected");
            manager = BookManager.Stub.asInterface(service);
        }

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

测试结果:

image.png

注意:两个项目中与aidl相关的文件的包路径必须一一对应,否则识别不了无法通讯

相关文章

网友评论

    本文标题:Android AIDL

    本文链接:https://www.haomeiwen.com/subject/brcxhhtx.html