Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XMPP : Asmack Connection PacketListener Issue...

I am using asmack api for the Android XMPP Integration...

After the successful authentication for the xmpp connection, I have used PacketListener for the incoming message notification.

My application is tab based application. When I am moving to chat activity by clicking on the friends list, my packet listener is added to the connection in OnCreate method.

The issue is that as many times Im moving to this screen that number of listeners are added and Im getting same message multiple times.

Anysolution for this Issue ?

PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
        PacketListener pListener = new PacketListener() {
            public void processPacket(Packet packet) {
                Message message = (Message) packet;
                if (message.getBody() != null) {
                    String fromName = StringUtils.parseBareAddress(message
                            .getFrom());
                    Log.i("XMPPClient", "Got text [" + message.getBody()
                            + "] from [" + fromName + "]");
                    messages.add(fromName + ":");
                    messages.add(message.getBody());
                    // Add the incoming message to the list view
                    mHandler.post(new Runnable() {
                        public void run() {
                            setListAdapter();
                        }
                    });
                }
            }
        };

        connection.addPacketListener(pListener, filter);
like image 809
Vishal Khakhkhar Avatar asked Dec 27 '22 21:12

Vishal Khakhkhar


2 Answers

@Vishal. You should not add this listener in any of your activity file, just add these listeners in your Android Service which should run with a single instance all the time.

Thanks

like image 87
Gaurav Arora Avatar answered Dec 30 '22 12:12

Gaurav Arora


I got the same issue. This is what I did,

OnResume()
{
connection.removePacketListener(yourlistener);
}
like image 26
user2285285 Avatar answered Dec 30 '22 11:12

user2285285