我正在创建一个连接到 Android 上的 XMPP 服务器的应用程序。我想保持连接直到用户注销。
我应该使用常规服务还是绑定(bind)服务来保持连接?
欢迎任何提示、建议和有用的信息。
最佳答案
我喜欢这个解释:
Started services are easy to program for simple one way interactions from an activity to a service, however, they require more complex and ad hoc programming for extended two-way conversations with their clients.
In contrast, bound services may be a better choice for more complex two-way interactions between activities and services. For example, they support two-way conversations.
所以,正如您所说,如果您想与服务交互,请使用绑定(bind)服务。使用启动服务(或 Intent 服务)你可以做到,只是它需要更复杂的编程。 (作者:道格拉斯·施密特:https://www.youtube.com/watch?v=cRFw7xaZ_Mg (11'10'')):
以下是帮助我理解的摘要(感谢 Doug):
最后,最后一个链接也对我有帮助: http://www.techotopia.com/index.php/An_Overview_of_Android_Started_and_Bound_Services
Started services 由其他应用程序组件(例如 Activity 甚至广播接收器)启动,并可能在后台无限期运行,直到服务停止或被 Android 运行时系统销毁为了释放资源。如果启动服务的应用程序不再在前台运行,服务将继续运行,即使最初启动服务的组件被销毁也是如此
绑定(bind)服务 类似于启动服务,不同之处在于启动服务通常不返回结果或不允许与启动它的组件进行交互。另一方面,绑定(bind)服务允许启动组件与服务交互并从服务接收结果。
关于android - 服务还是绑定(bind)服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9272217/