SỬ DỤNG AIDL HỖ TRỢ THÔNG TIN LIÊN TỤC CHO DỊCH VỤ
Trong chương 9, "Làm việc trong nền", bạn đã học cách để tạo ra dịch vụ cho các ứng dụng của bạn. Ở đây, bạn sẽ tìm hiểu làm thế nào để sử dụng các ngôn ngữ định nghĩa giao diện Android (AIDL) để hỗ trợ phong phú truyền thông liên tiến trình (IPC) giữa dịch vụ và compo¬nents ứng dụng khác, bao gồm các thành phần hoạt động trong các ứng dụng khác nhau hoặc trong quy trình riêng biệt.
Điều này cho phép bạn Dịch vụ khả năng hỗ trợ nhiều ứng dụng trên ranh giới quy trình.
để vượt qua các đối tượng giữa các quá trình, bạn cần phải tháo gỡ chúng thành nguyên thủy mức hệ điều hành hệ điều hành cơ bản sau đó có thể sắp xếp qua ranh giới ứng dụng. Điều này được thực hiện bằng cách thực hiện chúng như Parcelables.
AIDL được sử dụng để đơn giản hóa mã cho phép đối tượng trao đổi các quá trình của bạn. Nó tương tự như giao diện như COM hay CORBA ở chỗ nó cho phép bạn tạo ra các phương pháp công trong dịch vụ của bạn có thể chấp nhận và thông số trở về đối tượng và trả về giá trị giữa các quá trình.
Thực hiện một giao diện AIDL
AIDL hỗ trợ các kiểu dữ liệu sau đây:
nguyên thủy ngôn ngữ ► Java (int, boolean, float, char, vv.)
giá trị CharSequence. ► string và
đối tượng ► Danh sách (bao gồm cả thuốc generic), trong đó mỗi phần tử là một loại hỗ trợ. Lớp nhận sẽ luôn nhận được danh sách đối tượng cụ thể như một ArrayList.
Đối tượng ► Bản đồ (không bao gồm thuốc generic), nơi mà mỗi phím và phần tử là một loại hỗ trợ. Lớp tiếp nhận sẽ luôn nhận được những đối tượng đồ instantiated như một HashMap.
Giao diện ► AIDL tạo (bảo hiểm sau này). Một tuyên bố nhập khẩu là luôn luôn cần thiết cho các.
► Các lớp mà thực hiện các giao diện Parcelable (trong phần tiếp theo). Một tuyên bố nhập khẩu là luôn luôn cần thiết cho các.
Các phần sau đây chứng minh làm thế nào để làm cho lớp học của bạn Parcelable, tạo ra một định nghĩa AIDL Dịch vụ, thực hiện và phơi bày rằng định nghĩa dịch vụ để sử dụng bởi các thành phần ứng dụng khác.
Làm Lớp Parcelable
Để cho lớp học không có nguồn gốc để được thông qua giữa các quá trình, họ phải thực hiện giao diện Parcelable. Điều này cho phép bạn phân hủy các thuộc tính trong lớp học của bạn thành các loại nguyên thủy được lưu trữ trong một bưu kiện có thể được marshaled qua ranh giới quy trình.
Thực hiện các phương pháp writeToParcel để phân hủy đối tượng lớp học của bạn, bằng cách sử dụng phương pháp ghi * để tiết kiệm tài sản đối tượng vào các đối tượng Parcel đi:
công cộng làm mất hiệu lực writeToParcel (Parcel ra, int cờ) {out.writeLong (MyLong); out.writeString (myString); out.writeDouble (myDouble);
Để tái tạo một đối tượng mà đã được lưu như một bưu kiện, thực hiện các công trường tạo tĩnh (mà thực hiện một lớp Parcelable.Creator mới) để tạo ra một đối tượng mới dựa trên một bưu kiện đến bằng cách đọc đến bưu kiện sử dụng của nó đọc * phương pháp:
MyClass tin (Parcel in) {MyLong = in.readLong (); myString = in.readString (); myDouble = in.readDouble ();
}
Liệt 18-13 cho thấy một ví dụ cơ bản của việc sử dụng các giao diện Parcelable cho lớp Quake bạn đã sử dụng trong ví dụ động đất liên tục.
đang được dịch, vui lòng đợi..