-1

asmack + openfire develop a android IM app. But I donn't known how to receive message from friends then dispatch to corresponding chat.

I had the follow function: connection the openfire server and login success(acount A). Then I have a 'FriendsActivity' show all friends in ListView, click the item then intent to 'ChatActivity'. In 'ChatActivity' I create a chat to acount B(or another acount).

 chatManager.createChat(B, new MessageListener() {
    @Override
    public void processMessage(Chat chat2, 
               org.jivesoftware.smack.packet.Message msg) {
                       handler.obj = msg; //handler to updata the listview to show message history
            }
  }

The code help me implement chat with B, but A should click the item to 'ChatActivity' first.

How to receive message at 'FriendsActivity' and give diffrent hint like "you have a new message from acount C"(acount C is the friends of A). Then, when I click C item to 'Chat Activity can show the message from C.

Yuansheng liu
  • 165
  • 1
  • 2
  • 10

1 Answers1

0

this is actually an involved question and there are several things to consider;

Activities are destroyed when you press back so you'll most likely need a way of persistently storing data for that chat, a way that is available to each activity such as a database. (is that your handler class??).

So

  1. Connect and add the chatManager in the friends activity as per your code
  2. On message received (processMessage()) update the friends list by getting the JID from the message, cycling through the adapter data until you find the appropriate friend and updating that view some how. (remember if you're cycliing through the adapters base data like an ArrayList the position of the view in the list is not the position of the data as Android recycles views so you'll have to subtract list.getFirstVisiblePosition() from the position in your ArrayList data)
  3. Add an onClickListener() to each friend in the friends list. (utilise a custom adapter to dispay each friend)
  4. In the onClickListener, pass the chatActivity an intent containing the chat body object and the JID of the friend it was from. This is where having a database makes it easy as you could pass the chatActivity the JID in the intent and lookup all messages ascociated to that JID.

Hope that helps a little

James W
  • 410
  • 2
  • 10