什么时候在addSyncStanzaListener中调用ProcessPacket(smack 4.1.4)

时间:2015-10-18 07:35:52

标签: android xmpp smack

我正在尝试在我的android聊天应用程序中接收聊天消息,我正在使用smack 4.1.4库开发,我能够发送消息,但我无法接收任何消息。以下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;
}

我怎样才能解决问题?

2 个答案:

答案 0 :(得分:0)

尝试使用addPacketListener代替addSyncStanzaListener

PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
                    connection.addPacketListener(new PacketListener() {
            public void processPacket(Packet packet) {
                Message message = (Message) packet;
                if (message.getBody() != null) {
                    messageReceived(
                            StringUtils.parseBareAddress(message.getFrom()), 
                            message.getBody()
                            );
                }
            }
        }, filter); 

答案 1 :(得分:0)

使用以下过滤器:

FromMatchesFilter filter = FromMatchesFilter.createBare("id@jabber.org");
m_packet_collector = m_connection.createPacketCollector(filter);

Bare vs Full过滤器:裸格式,仅匹配“id@jabber.org”,而完整过滤器匹配“id@jabber.org/resoure”。