Android的使用啪加为好友加为好友、Android

2023-09-04 07:36:37 作者:尖酸刻薄

我是新来使用咂嘴库和使一个聊天应用。我做了高达多大程度和在这一步我想问两个问题。

当我加入一个朋友的朋友得到加在我的名单,但没有发送给谁我有加,如何实现相同的朋友任何通知。我已经加入了code以下。

我要问的第二件事是,我怎么能检查哪些我要添加该用户是否是应用程序的一部分,或者成员或没有(意味着它是在服务器上或没有)。让谁不是注册到应用程序的用户不应在好友列表中添加。

这里是code

 公共静态布尔addFriend(字符串JID){
            字符串nickname = NULL;
            绰号= StringUtils.parseBareAddress(JID);
            RosterEntry entry4 = roster.getEntry(samsad);
            如果(!roster.contains(JID)){
                尝试 {
                    presence认购=新的presence(presence.Type.subscribe);
                    subscribe.setTo(JID);
                    connection.sendPacket(认购);
                    roster.createEntry(JID,昵称,NULL);
                      //发送名册条目(任何)的user2
                    RosterExchangeManager REM =新RosterExchangeManager(连接);
                    REM.send(entry4,JID);
                    返回true;
                }赶上(XMPPException E){
                    通信System.err.println(中添加朋友错误);
                    返回false;
                }
            } 其他 {
                返回false;
            }
        }
 

在服务后台运行名册外汇经理

  / ** Remotr交换管理* /
             RosterExchangeManager REM =新RosterExchangeManager(连接);
              //创建一个RosterExchangeListener将迭代接收到的名册项
              RosterExchangeListener rosterExchangeListener =新RosterExchangeListener(){
                  公共无效entriesReceived(距离,迭代器remoteRo​​sterEntries字符串){
                      通知(接收== 4);
                      而(remoteRo​​sterEntries.hasNext()){
                          尝试 {
                              //获取所接收到的条目
                              RemoteRo​​sterEntry remoteRo​​sterEntry =(RemoteRo​​sterEntry)remoteRo​​sterEntries.next();
                              //显示在控制台上的遥控进入
                              的System.out.println(remoteRo​​sterEntry);
                              //添加条目的用户2的花名册
                              roster.createEntry(
                                  remoteRo​​sterEntry.getUser(),
                                  remoteRo​​sterEntry.getName(),
                                  remoteRo​​sterEntry.getGroupArrayNames());
                              通知(接收== 1);
                          }
                          赶上(XMPPException E){
                              e.printStackTrace();
                          }
                      }
                  }
              };
              rem.addRosterListener(rosterExchangeListener);
        }
        其他{
            showToast(连接丢失 - ,0);
        }
    }
 
30区 加好友一起玩 Android安卓游戏论坛

解决方案

1时,则必须先注册一个PacketListener为 presence.Type.subscribe 问题您连接到服务器。添加的所有过程,并接受我的朋友在这里 回答

2,您可以使用 UserSearch 类搜索特定的用户,如果用户没有在服务器上找到,那么你可以认为用户没有在服务器上注册。

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.

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.

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, 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.