2 服务通信
我有一个启动另一个远程服务的服务。当第二个服务启动时,它使用 IPC 绑定到第一个服务,一切都很好。
我的问题是,我希望第一个服务也能够与第二个服务通信(现在,只实现了相反的方式)。我可以用同一个 Binder 来做到这一点吗?或者我应该将第一个服务绑定到第二个服务? 也许有办法让两个服务在同一个连接上进行通信?
所有帮助将不胜感激。
弗拉德
I have a service that starts another remote service. when the second service have started, it binds to the first service using IPC, and all works great.
My question is, i would like the first service to be able to talk to the second service as well (right now, only the other way around is implemented). can i somehow do it with the same Binder? or should i bind the first service to the second one?
maybe there's a way to make two services talk on the same connection somehow?
All help would be appreciated.
Vlad
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您需要第一个
Service
来调用第二个Service
上的方法,那么您需要从第二个获取一个
。如果您只想进行通信,可以使用Binder
服务Broadcasts
并在第一个Service
中设置一个Receiver
。If you need the first
Service
to call methods on the secondService
, then you'll need to get aBinder
from the secondService
. If you just want to communicate, you could useBroadcasts
and set up aReceiver
in the firstService
.我认为使用同一个绑定器的双向通信对于服务来说是不可能的。我已经尝试了很多,并且我发现的问题,使用您自己的示例的说法是,从第二个服务检索的 IBinder 接口始终为空。
我的意思是,第一个服务必须绑定到第二个服务,以便获得有效的 IBinder 来与第二个服务通信。限制是,当第二个服务调用第一个服务时,它无法将接口传递给第一个服务,因此可以发生回调(从第一个服务到第二个服务)。
相反,需要建立与第二个服务的新的独立 IBinder 连接。希望有人能帮助我们解决这个问题。
阿尔贝托.
I think that two way communication using the same binder is impossible for services. I have tried a lot, and the problem I found, using the parlance of your own example is that IBinder interfaces retrieved from the second service are always null.
I mean, the first service would have bind to the second in order to obtain a valid IBinder to talk to the second service. The limitation is that when the second service calls the first, it cannot pass interfaces to the first service so callbacks(from first to second) can take place.
A new independent IBinder connection to the second service needs to be established, instead. Hope somebody can help us with this one.
Alberto.