Android Binder建模问题-MusicStore、IMusicStore.aidl、音乐、IMusic.aidl

发布于 2024-10-02 09:02:05 字数 1404 浏览 0 评论 0原文

Android Binder文档有一个关于如何通过Binder接口传递对象矩形的简单示例,我想知道如果对象本身有一些也是由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.
    }
    ...
}

Android Binder document has an simple example about how to pass an object Rect through Binder interface, I'm wondering how to do the modeling if the object itself has some methods which are also defined by AIDL interface?

For example, project A owns MusicStoreManager, project B owns MusicStore and Music, the interaction is through Binder IPC. I'm using IMusicStore.aidl defines a method "IMusic getMusic(int musicId)", and the IMusic.aidl defineds a method "byte[] getMusicData(int from, int to)", but I'm stuck here:

  1. How to model the Music class, the IMusic interface, and the IMusic.stub on the project B in general?

  2. Should or CAN the getMusic() method return a IMusic.Stub instance or Music instance in the following code?

  3. How to understand the IMusic.Stub?

  4. Does the class Music has to implement IMusic interface as well as Parcelable?

Many thanks - I'm really confused.

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.
    }
    ...
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撩动你心 2024-10-09 09:02:05

好吧,我可以告诉您一个可行的方法:对于通过 AIDL 定义的接口,始终创建 .Stub 类的实例。不要将该接口随意应用到其他类上。

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

项目 B 应该创建 IMusicStore.StubIMusic.Stub 的子类。 MusicStoreService 将从 onBind() 返回 IMusicStore.Stub 类的实例,因此项目 A 可以获得 IMusicStore > 通过 bindService() 进行代理。 IMusicStore.Stub 类上的 getMusic() 实现将返回 IMusic.Stub 类的实例。

Well, I can tell you a recipe that works: for interfaces defined via AIDL, always create instances of the .Stub class. Don't apply the interface randomly on other classes.

For example, project A owns MusicStoreManager, project B owns MusicStore and Music

Project B should be creating subclasses of IMusicStore.Stub and IMusic.Stub. MusicStoreService would return an instance of the IMusicStore.Stub class from onBind(), so Project A can get the IMusicStore proxy via bindService(). The implementation of getMusic() on the IMusicStore.Stub class would return an instance of an IMusic.Stub class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文