7

我有一个 .aidl 文件,它定义了一个接口类型的单个 parcelable,比方说

parcelable MyInterface;

其中 MyInterface 是在 MyInterface.java 中声明的一个 java 接口,它扩展了 Parcelable 接口。android parcelable 机制要求我在 parcelable 类中定义一个静态 CREATOR。但是,由于接口类不知道具体实现,因此无法实现 createFromParcel() 方法,我该如何为接口执行此操作?

android 运行时将如何决定调用哪个 CREATOR(来自哪个子类)?甚至不可能在 .aidl 文件中使用接口类型吗?

4

3 回答 3

7
于 2014-06-18T10:25:43.690 回答
3
  1. 关于 AIDL 文件中的使用界面:我认为没有什么可以阻止您这样做。因为“parcelable MyInterface;” 实际上并不会在 gen 文件夹中生成任何内容,它只是使用此 MyInterface 类型的任何 AIDL 接口的函数签名所需要的。

  2. CREATOR 您必须为所有实现 android.os.Parcelable 的类添加创建者定义。

于 2014-06-18T09:49:36.677 回答
0

我遇到了类似的情况,并想分享我所做的。我有以下aidl主界面,其中包含另一个界面。

//ICounterAidlInterface.aidl

import path.to.aidldirectory.CounterListener

interface ICounterAidlInterface {
    int getCounter();
    void setCounterListener(CounterListener listener); 
}

不要忘记导入。

问题是如何表示这种新类型:CounterListener. 由于CounterListener它本身就是一个接口,所以不需要将其标记为 parcelable。

您也需要为该文件创建另一个aidl 文件CounterListener。所以,我创建了另一个aidl文件:

//CounterListener.aidl
interface CounterListener{
    void newData(int data);
}

希望这可以帮助 :)

于 2017-01-14T23:58:01.457 回答