【问题标题】:Chat Application - Smack 4.1 Android IntentService - send message聊天应用程序 - Smack 4.1 Android IntentService - 发送消息
【发布时间】:2015-10-04 06:01:32
【问题描述】:

我正在使用 Smack 4.1 在 Android 中创建一个聊天应用程序。我已经实现了一个 IntentService,一旦用户登录应用程序,它就会创建连接并在 xmpp 中登录用户。我正在使用以下代码从片段启动意图服务,效果很好:

chatIntent = new Intent(mContext, ChatService.class);
chatIntent.setAction(XMPP_ACTION_CONNECT);
mContext.startService(chatIntent);

然后我尝试从片段发送消息。我尝试使用与上面相同的代码

Intent sendmsgIntent = new Intent(mContext, ChatService.class);
sendmsgIntent.putExtra("msg",messageText);
sendmsgIntent.putExtra("to",companionLabel.getText().toString());
sendmsgIntent.setAction(ACTION_SEND_MESSAGE);
mContext.startService(sendmsgIntent);

但是在 IntentService 中接收到意图时,XMPPTCPConnection 连接为空,我无法使用:

connection.sendStanza(message);

为什么这个是空的?服务是否从头开始? 这是我用来发送第二个意图的方式错误吗? 或者我如何获得在发送第一个意图时创建的 XMPPTCPConnection 连接?

【问题讨论】:

    标签: android xmpp chat smack


    【解决方案1】:

    是的,IntentService 只是在后台线程中执行 onHandleIntent 然后被销毁。

    改为使用可以从片段和活动绑定的普通服务,以使其在其生命周期中可用;实际上有一些不错的文档: http://developer.android.com/guide/components/bound-services.html#Binding 该服务将在 UI 线程上运行,因此您需要在新线程或 AsyncTasks 中执行 Smack 网络调用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-19
      • 1970-01-01
      相关资源
      最近更新 更多