【问题标题】:How to use Parcelable in AIDL如何在 AIDL 中使用 Parcelable
【发布时间】:2017-05-14 09:13:36
【问题描述】:

我定义了一个实现Parcelable的类,像这样:

public class Book implements Parcelable

而且我知道我可以像这样在 AIDL 文件中使用这个类:

import com.okay.demo.aidl.bean.Book;
interface IBookManager {
   void addBook(in Book book);
}

但我想知道的是,如果我不知道实现类,我可以只在 AIDL 文件中 Parcelable 吗?我可以像这样使用Parcelable吗:

interface IBookManager {
   void addData(in Parcelable data);
}

我曾尝试将Book 替换为Parcelable,但没有成功。

还是不能在 AIDL 中做到这一点?

【问题讨论】:

  • 不,你不能在这里使用Parcelable接口,它必须是一个实现这个接口的类
  • 当然,您可以像这样使用 Parcelable for IPC,只是不能使用开箱即用的 AIDL 工具。看看我写的这个库:github.com/chdir/aidl2。它生成代码,直接使用 Binder 对象在进程之间传递运行时定义的 Parcelables、Serializables 甚至其他 IPC 接口。
  • @pskink:你是怎么发现它不可能的?你有任何代码/文档参考吗?
  • “你有任何代码/文档参考吗?” - @Yash 也许 4 年前我会记得...... ;-)
  • 呵呵..我今天遇到了同样的问题,希望有一个权威的答案。它当然不是为我编译的。

标签: android parcelable aidl


【解决方案1】:

Android documentation 说你应该为你的班级Book 创建一个 AIDL 文件。你的Book.aidl 应该是这样的:

import com.okay.demo.aidl.bean.Book;

// Declare Rect so AIDL can find it and knows that it implements
// the parcelable protocol.
parcelable Book;

有了这个新文件,您现在可以在 AIDL 文件中使用 Book

import com.okay.demo.aidl.bean.Book;

interface IBookManager {
   void addData(in Book book);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-08
    • 2018-04-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多