2
votes

I developed a chat app using Smack by XMPP server(openFire). For getting incoming message, i used to PacketFilter This is incoming msg code:-

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);

But i want to remove paketListener during activity close or stop or destroy. i follow this http://community.igniterealtime.org/thread/28921 for remove packet listeners But not found solution.

please check. thank you for your timing.

2
Is there a reason for not using Connection.removePacketListener?harism
not, but how can i use. because i have not object of packetListener.user2160008
So i want to use in onStop() .user2160008
You would need to store the PacketListener instance within your class variables instead of creating a local instance only.harism
thx, but i dont want to do. may here other way?user2160008

2 Answers

2
votes

Here's something you could start with:

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

Then later on once you want to instantiate the PacketListener:

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

And for removing it:

connection.removePacketListener(packetListener);
0
votes

You can try this, it worked for me:`

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);