【发布时间】: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 连接?
【问题讨论】: