2

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.

9
  • Is there a reason for not using Connection.removePacketListener?
    – harism
    Commented Apr 23, 2013 at 8:06
  • not, but how can i use. because i have not object of packetListener. Commented Apr 23, 2013 at 8:08
  • So i want to use in onStop() . Commented Apr 23, 2013 at 8:09
  • You would need to store the PacketListener instance within your class variables instead of creating a local instance only.
    – harism
    Commented Apr 23, 2013 at 8:10
  • thx, but i dont want to do. may here other way? Commented Apr 23, 2013 at 8:11

2 Answers 2

2

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);
1
  • if i remove my listener after receiving only one message it only receives one message. but the server side offline messages gets empty. i am using ejabberd server. can you tell me why ? @harism Commented Apr 26, 2016 at 11:46
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.