Reputation: 2116
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);
Upvotes: 4
Views: 5763
Reputation: 21117
You should not register listener in your activity life cycle. Instead create a singleton of Connection controller and add this listener into that controller.
Upvotes: 1
Reputation: 24083
I am not sure if I have a complete overview of the structure of your app, but why don't you simply call XMPPConnection.removePacketListener() when appropriately?
Upvotes: 2
Reputation: 41
I got the same issue. This is what I did,
OnResume()
{
connection.removePacketListener(yourlistener);
}
Upvotes: 2
Reputation: 8362
@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
Upvotes: 5
Reputation: 8659
I think a better way is to depend on the ChatManager
to manage your chats, send, and receive chats
this what I do In my app:
// Assume we've created a Connection name "connection".
ChatManager chatmanager = connection.getChatManager();
Chat newChat = chatmanager.createChat("[email protected]",
new MessageListener() {
public void processMessage(Chat chat, Message message) {
System.out.println("Received message: " + message);
}
});
try {
newChat.sendMessage("Howdy!");
}
catch (XMPPException e) {
System.out.println("Error Delivering block");
}
to recive a message that you stated
// Assume a MessageListener we've setup with a chat.
public void processMessage(Chat chat, Message message) {
// Send back the same text the other user sent us.
chat.sendMessage(message.getBody());
}
to listen for other incoming chats
// Assume we've created a Connection name "connection".
ChatManager chatmanager = connection.getChatManager().addChatListener(
new ChatManagerListener() {
@Override
public void chatCreated(Chat chat, boolean createdLocally)
{
if (!createdLocally)
chat.addMessageListener(new MyNewMessageListener());;
}
});
this will make it cleaner than packets filters
to avoid multiple event listeners registrations register your listener in onResume()
method then unregister in onPause()
Upvotes: 0