Android ADD FRIEND using Smack

情到浓时终转凉″ 提交于 2019-11-30 15:38:57

问题


I am new to the use of smack library and making one chatting application. I have made upto much extent and at this step i want to ask two questions.

  1. when i add a friend the friend got added in my list but there is not any notification sent to the FRIEND whom i have added, How to achieve the same. I have added the code below.

  2. The second thing i want to ask is that how can I check whether the user which I am going to add is a part or member of the app or not ( mean it is on the server or not). So that the user who is not registered to the app should not be added in the friends list.

here is the code

public static boolean addFriend(String jid) {
            String nickname = null;
            nickname = StringUtils.parseBareAddress(jid);
            RosterEntry entry4 = roster.getEntry("samsad");
            if (!roster.contains(jid)) {
                try {
                    Presence subscribe = new Presence(Presence.Type.subscribe);
                    subscribe.setTo(jid);               
                    connection.sendPacket(subscribe);               
                    roster.createEntry(jid, nickname, null);
                      // Send a roster entry (any) to user2
                    RosterExchangeManager REM = new RosterExchangeManager(connection);
                    REM.send(entry4, jid);
                    return true;
                } catch (XMPPException e) {
                    System.err.println("Error in adding friend");
                    return false;
                }
            } else {
                return false;
            }
        }

Roster Exchange manager running in the service in background

/**Remotr Exchange Manager*/
             RosterExchangeManager rem = new RosterExchangeManager(connection);
              // Create a RosterExchangeListener that will iterate over the received roster entries
              RosterExchangeListener rosterExchangeListener = new RosterExchangeListener() {
                  public void entriesReceived(String from, Iterator remoteRosterEntries) {
                      notification("Receive==4");
                      while (remoteRosterEntries.hasNext()) {
                          try {
                              // Get the received entry
                              RemoteRosterEntry remoteRosterEntry = (RemoteRosterEntry) remoteRosterEntries.next();
                              // Display the remote entry on the console
                              System.out.println(remoteRosterEntry);
                              // Add the entry to the user2's roster
                              roster.createEntry(
                                  remoteRosterEntry.getUser(),
                                  remoteRosterEntry.getName(),
                                  remoteRosterEntry.getGroupArrayNames());
                              notification("Receive==1");   
                          }
                          catch (XMPPException e) {
                              e.printStackTrace();
                          }
                      }
                  }
              };
              rem.addRosterListener(rosterExchangeListener);
        }
        else{
            showToast("Connection lost-",0);
        }
    }

回答1:


1, The problem is you must register a PacketListener for Presence.Type.subscribe before you connect to server. All the process of add and accept friend i answered in here

2, You can use UserSearch class to search for a specific user and if user is not found on server then you can assume that user is not registered on server.



来源:https://stackoverflow.com/questions/12812855/android-add-friend-using-smack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!