访问两个绑定服务中的远程服务方法
我有两个服务 A 和 B。如果我们想在服务 A 中访问服务 B 的某些方法,我们必须创建一个 ServiceConnection 类,在服务 A 中我们必须创建 Stub 并实现这些方法。
但我也想从服务 B 访问服务 A 的方法。为了试用,我又创建了一个 aidl 连接并实现了 ServiceConnection,它工作正常,这是唯一的方法还是我们可以使用现有服务连接和通过其他方式实现此功能?
I have two services A and B. If we want to access some methods of service B in service A we have to create a ServiceConnection class and inside service A we have to create Stub and implements the methods.
But I also want to access the methods of service A from service B. For trial I created one more aidl connection and implemented ServiceConnection it works fine, is it the only way OR we can use the existing serviceconnection and implement this functionality by other way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据我的经验,我认为这是 AIDL 工作的唯一方式。您始终可以创建一个自定义回调,该回调可以从服务 A 传递到服务 B,然后当 B 想要发送回 A 时,它可以触发回调。还可以选择来回传递意图或通过套接字进行通信。只要确保你在另一边阻挡即可。
I would assume this is the only way AIDL works from my experience. You could always create a custom callback that could be passed into service B from service A, then when B would like to send back to A it could trigger the callback. There is also the option of passing intents back and forth or communication through a socket. Just make sure you are blocking on the other side.
服务当然可以有回调接口(即服务可以调用客户端中的方法)。
建议您参考 RemoteService 示例在SDK中。
Services can certainly have callback interfaces (i.e. so services can call methods in clients).
Suggest you refer to the RemoteService example in the SDK.