【发布时间】:2026-01-09 12:40:01
【问题描述】:
我正在尝试在我正在使用 smack 4.1.4 库开发的 android 聊天应用程序中接收聊天消息,我可以发送消息但我无法接收任何消息。 ProcessPacket 下面的方法没有被调用。这是我的代码:
protected Void doInBackground(String... urls) {
StanzaFilter filter = new StanzaTypeFilter(org.jivesoftware.smack.packet.Message.class);
connection.addSyncStanzaListener(new StanzaListener() {
public void processPacket(Stanza stanza) {
Message message = (Message) stanza;
if (message.getBody() != null) {
String fromName = message.getFrom();
chatMessage newMsg = new chatMessage(message.getBody(), "in");
chatList.add(newMsg);
Log.i("XMPPClient", "Got text [" + message.getBody() + "] from [" + fromName + "]");
myadapter = new chatAdapter(actualChatActivity.this, R.layout.single_message, chatList);
chatListView.setAdapter(myadapter);
}
}
}, filter);
return null;
}
public Void setConnection(XMPPConnection connection)
{
this.connection=connection;
if (connection != null) {
// Add a packet listener to get messages sent to us
new ReceiveMessageTask().execute();
}
return null;
}
我该如何解决我的问题?
【问题讨论】: