0

Android Binder 文档有一个关于如何通过 Binder 接口传递对象 Rect 的简单示例,我想知道如果对象本身有一些也由 AIDL 接口定义的方法,如何进行建模?

比如项目A拥有MusicStoreManager,项目B拥有MusicStore和Music,交互是通过Binder IPC。我正在使用 IMusicStore.aidl 定义了一个方法“IMusic getMusic(int musicId)”,而 IMusic.aidl 定义了一个方法“byte[] getMusicData(int from, int to)”,但我被困在这里:

  1. 一般如何对项目B上的Music类、IMusic接口和IMusic.stub进行建模?

  2. getMusic() 方法是否应该或可以在以下代码中返回 IMusic.Stub 实例或 Music 实例?

  3. 如何理解 IMusic.Stub?

  4. Music 类是否必须实现 IMusic 接口以及 Parcelable?

非常感谢 - 我真的很困惑。

public class MusicStoreService extends Service {
    ...
    protected static final IMusicStore.Stub store = new IMusicStore.Stub() {
        ...
        public IMusic getMusic(int id) throws RemoteException {
            return new Music(id); // or return new IMusic.Stub() ???
        }
    }
    ...
    protected static final IMusic.Stub music = new IMusic.Stub() {
        ...
        public byte[] getMusicData(int from, int to) throws RemoteException {
            // open the associated file, read the data within range, return it back.
        }
    }
    ...
}

public class Music extends Object implements Parcelable, IMusic {
    ...
    public byte[] getMusicData(int from, int to) throws RemoteException {
        // open the associated file, read the data within range, return it back.
    }
    ...
}
4

1 回答 1

0

好吧,我可以告诉你一个有效的方法:对于通过 AIDL 定义的接口,总是创建.Stub类的实例。不要在其他类上随意应用接口。

例如,项目 A 拥有 MusicStoreManager,项目 B 拥有 MusicStore 和 Music

项目 B 应该创建 和 的子IMusicStore.StubIMusic.Stub。将从 中返回该类MusicStoreService的一个实例,因此项目 A 可以通过. 类上的实现将返回一个类的实例。IMusicStore.StubonBind()IMusicStorebindService()getMusic()IMusicStore.StubIMusic.Stub

于 2010-11-13T01:29:10.700 回答