java - 在 Android 中创建信使

标签 java android irc

我是(有点)Android 开发新手,我想创建一个基于 PircBot 库的 Android Messenger 应用程序。 我现在知道有几个限制条件:

  • 必须使用 IRC,现在可能无法更改,因为它已经在要求中
  • IRC 服务需要在自己的进程中运行,这样它就不会在收听 IRC 消息时干扰 UI,并且它应该在后台接收消息并在不显示 UI 的情况下发送通知
  • 我在不同的屏幕上使用不同的 Activity,因此所有 Activity 都需要使用该服务(登录、加入 channel 、发送/接收消息),并且应根据当前的连接状态通知不同的 Activity。<

我已经尝试从我的 Activity 中绑定(bind)服务,但绑定(bind)器似乎只适用于同一进程中的服务。 (我不确定这个,搜索没有返回任何有用的东西)

为了满足所有约束条件,构建项目的首选方法是什么?我还可能遇到其他什么问题?

抱歉英语不好

最佳答案

IRC service needs to run in its own process so it doesn't interfere with UI while listening to IRC

我看不出 IRC 服务需要在其自己的进程中运行的任何原因。根据定义,服务是不与用户交互的元素,如 docs 中所述。 :

A Service is an application component representing either an application's desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use.

而且,正如您所说,通过让此服务在其自己的进程中运行,就不可能 bind到它(实际上,您需要在此时执行 IPC communication)。


I'm using different activities for different screens so all activities will need to use the service (logged in, joined channel, send/recieve message) and different activities should be notified based on the current state of the connection.

要满足此要求,您可以简单地使用 LocalBroadcastManager共享从服务​​到所有 Activity 的信息。您的 Activity 可以为它们希望听到的事件(connectionStateChange、MessageReceived 等)设置 BroadcastReceivers,并据此采取行动。


最后,因为阅读其他人的代码总是一个好主意,请查看友好的搜索引擎为您提供的这两个相似的项目:

关于java - 在 Android 中创建信使,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17128058/

相关文章:

java - 持久性逻辑应该放置在域模型 bean 中还是仅放置在 DAO 中?

C++ 套接字连接错误

java - getApplicationContext 导致 NullPointerException

Java TCP 监听器循环

irc - Weechat 获取配置变量

java - 如何检查是否设置了表单或值?

java - 在 Java 中添加到 ArrayList 中的元素?

java - Note : An exception occurred while getting the Javadoc. 详见日志-eclipse

java - Android Intent 创建对象的新实例

java - Eclipse在Android上导入MoPub SDK报错