2

我使用Smack by XMPP server(openFire)开发了一个聊天应用程序。为了获取传入的消息,我使用PacketFilter 这是传入的消息代码:-

PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
        connection.addPacketListener(new PacketListener() {
            public void processPacket(Packet packet) {
                Message message = (Message) packet;
                incomemsg = message.getBody();
                if (incomemsg != null) {
                    String fromName = StringUtils.parseBareAddress(message.getFrom());
                    frmname =  message.getBody();
                    Log.i("XMPPClient", "Got text [" + message.getBody() + "] from [" + fromName + "]");
                    // Add the incoming message to the list view
                    mHandler.post(new Runnable() {
                        public void run() {
                            // show the msg of chat in right side.
                              showMessage(frmname,false);
                              scrollContainer.getFocusables(scrollContainer.getChildCount());
                        }
                    });

                }
            }
        }, filter);

但我想在活动关闭或停止或销毁期间删除 paketListener。我按照这个http://community.igniterealtime.org/thread/28921删除数据包侦听器但没有找到解决方案。

请检查。谢谢你的时间。

4

2 回答 2

2

您可以从以下内容开始:

public class YourClass {
    private PacketListener packetListener;
    ....
}

然后稍后一旦你想实例化 PacketListener:

packetListener = new PacketListener(
....
);
connection.addPacketListener(packetListener);

并删除它:

connection.removePacketListener(packetListener);
于 2013-04-23T08:21:42.380 回答
0

你可以试试这个,它对我有用:`

PacketFilter filter;
PacketListener packetListener;
filter = new MessageTypeFilter(Message.Type.chat);
packetListener = new PacketListener() {
                                public void processPacket(Packet packet) {
                                    // do something with your packet here
                                    Message message = (Message)packet;
                                    String from  = message.getFrom();
                                    String body = message.getBody();
                                    System.out.println("Message from: " + from + " " + body);
                                }
                            };
                            connection.addPacketListener(packetListener, filter);
//And when you want to remove it:
connection.removePacketListener(packetListener);
于 2013-07-17T17:28:36.530 回答