Android Binder建模问题-MusicStore、IMusicStore.aidl、音乐、IMusic.aidl
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)”,但我被困在这里:
一般如何在项目 B 上对 Music 类、IMusic 接口和 IMusic.stub 进行建模?
getMusic() 方法是否应该或可以在以下代码中返回 IMusic.Stub 实例或 Music 实例?
如何理解IMusic.Stub?
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:
How to model the Music class, the IMusic interface, and the IMusic.stub on the project B in general?
Should or CAN the getMusic() method return a IMusic.Stub instance or Music instance in the following code?
How to understand the IMusic.Stub?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我可以告诉您一个可行的方法:对于通过 AIDL 定义的接口,始终创建
.Stub
类的实例。不要将该接口随意应用到其他类上。项目 B 应该创建
IMusicStore.Stub
和IMusic.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.Project B should be creating subclasses of
IMusicStore.Stub
andIMusic.Stub
.MusicStoreService
would return an instance of theIMusicStore.Stub
class fromonBind()
, so Project A can get theIMusicStore
proxy viabindService()
. The implementation ofgetMusic()
on theIMusicStore.Stub
class would return an instance of anIMusic.Stub
class.