机器人双向业务通信 [英] Android two-way service communication

查看:110
本文介绍了机器人双向业务通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动,当地的绑定服务。我已经得到了绑定服务并运行良好,这样我就可以处理通信从客户机到服务器,通过功能的服务器/粘合剂界面。

不过,我需要的双向通信,在某些时候该服务将需要通知的活动(与用户)的东西如发生,并需要等待,然后再继续用户操作。换句话说,我需要的服务器调用客户机为同步或阻断。即当服务调用函数它会通过几个参数移交给客户端,这将触发AlertDialog为要使用的用户,他们的选项将决定返回状态code发送回服务。直到此时,我希望该服务停止,等待,直到它已收到了回应,这将决定它做什么从那里(服务继续运行,服务关闭等。)

那么,有没有办法在Android中做到这一点?我需要引用我的活动,也需要公开这些阻塞方法,所以我可以从服务打电话给他们,然后等待响应。我有我的客户端活动的界面,但我挣扎于如何得到这个交给服务。我不希望使用序列化,并添加引用作为额外的意图,因为在客户端和服务有许多成员,我不希望序列化。

感谢。

解决方案
  1. 您可以使用广播,向服务发送信息到活动。
  2. startService的每个呼叫()活动类中只会叫的 onStartCommand()服务类方法,这样你就可以发送信息给它里面的意向。​​
  3. 使用服务绑定,以使自己的信息传递。

I have an activity and a local bound service. I've got the bound service up and running fine, so I can handle communication from the client to the server, through functions on the server/binder interface.

However I need two way communication, the service at some point will need to notify the activity (and the user) that something as happened, and needs to wait for a user action before proceeding. In other words, I need the servers calls to the clients to be synchronous or blocking. I.e. when the service calls a function it will pass a few parameters over to the client, which will trigger an AlertDialog for the user to use, and their options will determine the return status code sent back to the service. Until this point, I want the Service to halt and wait until it has recieved the response, which will then determine what it does from there (service continues to run, service shuts down etc.)

So is there a way in Android to do this? I need to get a reference to my activity, and it also needs to expose these blocking methods so I can call them from the service and then wait for a response. I've got an interface for my client activity, but I'm struggling on how to get this over to the service. I don't want to use Serialization and add the reference as an Intent extra, as both the client and service have many members which I do not want to be Serializable.

Thanks.

解决方案

  1. You can use BroadCasts to send info from service to Activity.
  2. Every call of startService() inside Activity class will just call onStartCommand() method of service class, so you can send message to it inside Intent.
  3. Use Service binding to make your own info transfer.

这篇关于机器人双向业务通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆