特征对象
AIDL objects can be sent either as a concrete AIDL type or as the type-erased IBinder interface:
birthday_service/aidl/com/example/birthdayservice/IBirthdayInfoProvider.aidl:
1. package com.example.birthdayservice;
3. interface IBirthdayInfoProvider {
4. String name();
5. int years();
6. }
birthday_service/aidl/com/example/birthdayservice/IBirthdayService.aidl:
1. import com.example.birthdayservice.IBirthdayInfoProvider;
3. interface IBirthdayService {
4. /** The same thing, but using a binder object. */
5. String wishWithProvider(IBirthdayInfoProvider provider);
7. /** The same thing, but using `IBinder`. */
8. String wishWithErasedProvider(IBinder provider);
9. }
birthday_service/src/client.rs:
1. /// Rust struct implementing the `IBirthdayInfoProvider` interface.
2. struct InfoProvider {
3. name: String,
4. age: u8,
5. }
7. impl binder::Interface for InfoProvider {}
9. impl IBirthdayInfoProvider for InfoProvider {
10. fn name(&self) -> binder::Result<String> {
11. Ok(self.name.clone())
12. }
14. fn years(&self) -> binder::Result<i32> {
15. Ok(self.age as i32)
16. }
17. }
19. fn main() {
20. binder::ProcessState::start_thread_pool();
21. let service = connect().expect("Failed to connect to BirthdayService");
23. // Create a binder object for the `IBirthdayInfoProvider` interface.
24. let provider = BnBirthdayInfoProvider::new_binder(
25. InfoProvider { name: name.clone(), age: years as u8 },
26. BinderFeatures::default(),
27. );
29. // Send the binder object to the service.
30. service.wishWithProvider(&provider)?;
32. // Perform the same operation but passing the provider as an `SpIBinder`.
33. service.wishWithErasedProvider(&provider.as_binder())?;
34. }
- Note the usage of
BnBirthdayInfoProvider. This serves the same purpose asBnBirthdayServicethat we saw previously.
