流水不争先,争的是滔滔不绝

Android(xmpp+smack+openfire)高仿QQ即时通讯【4】监听别人的添加好友申请与好友删除的设计

openfire 云聊IM 1654℃

先介绍一个方法类,主要是添加好友等方法。

public class XmppService{

    /** 
     * 删除当前用户 
     * @param connection 
     * @return 
     */  
    public static boolean deleteAccount(XMPPConnection connection)  
    {  
        try {  
            connection.getAccountManager().deleteAccount();         
            return true;  
        } catch (Exception e) {  
            return false;  
        }  
    }  
    /**
     * 返回所有组信息 
     * @return List(RosterGroup)
     */
    public static List getGroups(Roster roster) {
        List groupsList = new ArrayList();
        Collection rosterGroup = roster.getGroups();
        Iterator i = rosterGroup.iterator();
        while (i.hasNext())
            groupsList.add(i.next());
        return groupsList;
    }

    /**
     * 返回相应(groupName)组里的所有用户
     * @return List(RosterEntry)
     */
    public static List getEntriesByGroup(Roster roster,
            String groupName) {
        List EntriesList = new ArrayList();
        RosterGroup rosterGroup = roster.getGroup(groupName);
        Collection rosterEntry = rosterGroup.getEntries();
        Iterator i = rosterEntry.iterator();
        while (i.hasNext())
            EntriesList.add(i.next());
        return EntriesList;
    }

    /**
     * 返回所有用户信息 
     * @return List(RosterEntry)
     */
    public static List getAllEntries(Roster roster) {
        List EntriesList = new ArrayList();
        Collection rosterEntry = roster.getEntries();
        Iterator i = rosterEntry.iterator();
        while (i.hasNext())
            EntriesList.add(i.next());
        return EntriesList;
    }
    
    
    /** 
     * 创建一个组 
     */ 
    public static boolean addGroup(Roster roster,String groupName)  
    {  
        try {  
            roster.createGroup(groupName);  
            return true;  
        } catch (Exception e) {  
            e.printStackTrace();  
            return false;  
        }  
    }  
      
    /** 
     * 删除一个组 
     */  
    public static boolean removeGroup(Roster roster,String groupName)  
    {  
        return false;  
    }
    
    /**
     * 添加一个好友  无分组
     */
    public static boolean addUser(Roster roster,String userName,String name)
    {
        try {
            roster.createEntry(userName, name, null);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        
    }
    /**
     * 添加一个好友到分组
     * @param roster
     * @param userName
     * @param name
     * @return
     */
    public static boolean addUsers(Roster roster,String userName,String name,String groupName)
    {
        try {
            roster.createEntry(userName, name,new String[]{ groupName});
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        
    }
    
    /**
     * 删除一个好友
     * @param roster
     * @param userJid
     * @return
     */
    public static boolean removeUser(Roster roster,String userJid)
    {
        try {
            RosterEntry entry = roster.getEntry(userJid);
            roster.removeEntry(entry);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }       
    }
    
    /**
     * 把一个好友添加到一个组中
     * @param userJid
     * @param groupName
     */
    public static void addUserToGroup(final String userJid, final String groupName,
            final XMPPConnection connection) {
                RosterGroup group = connection.getRoster().getGroup(groupName);
                // 这个组已经存在就添加到这个组,不存在创建一个组
                RosterEntry entry = connection.getRoster().getEntry(userJid);
                try {
                    if (group != null) {
                        if (entry != null)
                            group.addEntry(entry);
                    } else {
                        RosterGroup newGroup = connection.getRoster().createGroup("我的好友");
                        if (entry != null)
                            newGroup.addEntry(entry);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
    }

    /**
     * 把一个好友从组中删除
     * @param userJid
     * @param groupName
     */
    public static void removeUserFromGroup(final String userJid,final String groupName, final XMPPConnection connection) {
            RosterGroup group = connection.getRoster().getGroup(groupName);
            if (group != null) {
                try {
                    RosterEntry entry = connection.getRoster().getEntry(userJid);
                    if (entry != null)
                        group.removeEntry(entry);
                } catch (XMPPException e) {
                    e.printStackTrace();
                }
            }
     }
    
    /** 
     * 修改心情 
     * @param connection 
     * @param status 
     */  
    public static void changeStateMessage(final XMPPConnection connection,final String status)  
    {  
        Presence presence = new Presence(Presence.Type.available);  
        presence.setStatus(status);  
        connection.sendPacket(presence);      
    }  
}

当对方发送好友申请的时候,主要监听代码:

roster.addRosterListener(  
     new RosterListener() {   
            @Override  
            //监听好友申请消息
            public void entriesAdded(Collection invites) {  
                // TODO Auto-generated method stub  
                System.out.println("监听到好友申请的消息是:"+invites); 
                for (Iterator iter = invites.iterator(); iter.hasNext();) {
                      String fromUserJids = (String)iter.next();
                      System.out.println("fromUserJids是:"+fromUserJids);
                      fromUserJid = fromUserJids;
                }                    
                 if(fromUserJid!=null){
                   Intent intent = new Intent();
                   intent.putExtra("USERID", pUSERID);
                   intent.putExtra("fromUserJid", fromUserJid);
                   intent.setClass(FriendListActivity.this, FriendListActivity.class);
                   startActivity(intent); 
               }     
            }    
            @Override  
            //监听好友同意添加消息
            public void entriesUpdated(Collection invites) {  
                    // TODO Auto-generated method stub  
                   System.out.println("监听到好友同意的消息是:"+invites);  
                   for (Iterator iter = invites.iterator(); iter.hasNext();) {
                      String fromUserJids = (String)iter.next();
                      System.out.println("同意添加的好友是:"+fromUserJids);
                      toUserJid = fromUserJids;
                    } 
                    if(toUserJid!=null){
                        XmppService.addUserToGroup(toUserJid, pGROUPNAME, connection);
                        loadFriend();
                    }                       
            } 
            @Override  
            //监听好友删除消息
            public void entriesDeleted(Collection delFriends) {  
                // TODO Auto-generated method stub  
                System.out.println("监听到删除好友的消息是:"+delFriends);  
                if(delFriends.size()>0){
                    loadFriend();
                }  
            } 
           @Override  
           //监听好友状态改变消息
            public void presenceChanged(Presence presence) {  
                // TODO Auto-generated method stub  
                friendMood = presence.getStatus();
                System.out.println("presence.getStatus()是:"+presence.getStatus());
    }     
                  
}); 

删除好友的设计:

主要代码:

/**
 * 长按事件删除好友
 */
@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
  super.onCreateContextMenu(menu, v, menuInfo);
  if(menuInfo instanceof ExpandableListView.ExpandableListContextMenuInfo){
    
    ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo) menuInfo;
    
    int type = ExpandableListView.getPackedPositionType(info.packedPosition);
    
    if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
      
      int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);
      int childPos = ExpandableListView.getPackedPositionChild(info.packedPosition);
      final FriendInfo dInfo = groupList.get(groupPos).getFriendInfoList().get(childPos);
      final GroupInfo  gInfo = groupList.get(groupPos);
      LayoutInflater layoutInflater= LayoutInflater.from(this);
            View delFriendView = layoutInflater.inflate(R.layout.dialog_del_friend, null);
            TextView delname = (TextView)delFriendView.findViewById(R.id.delname);
            delname.setText(dInfo.getJid());
            final CheckBox delCheckBox = (CheckBox)delFriendView.findViewById(R.id.delCheckBox);
            Dialog dialog =new AlertDialog.Builder(this)
            .setIcon(R.drawable.default_head)
            .setTitle("删除好友")
            .setView(delFriendView)
            .setPositiveButton("确定", new DialogInterface.OnClickListener(){
                @Override
                public void onClick(DialogInterface dialog, int which){
           XmppService.removeUserFromGroup(dInfo.getJid(), gInfo.getGroupName(), connection);
           if(delCheckBox.isChecked()){
             XmppService.removeUser(roster, dInfo.getJid());
           }
           Intent intent = new Intent();
                       intent.putExtra("USERID", pUSERID);
                     intent.putExtra("fromUserJid", CHECK);
                   intent.setClass(FriendListActivity.this, FriendListActivity.class);
                   startActivity(intent); 
                }
            })
            .setNegativeButton("取消", new DialogInterface.OnClickListener(){
                @Override
                public void onClick(DialogInterface dialog, int which){
                    dialog.cancel();
                  }
            })
            .create();
            dialog.show();
    }
  }
} 

本文转自:http://blog.csdn.net/yuanqihesheng/article/details/8875744

版权声明:部分文章、图片等内容为用户发布或互联网整理而来,仅供学习参考。如有侵犯您的版权,请联系我们,将立刻删除。
点击这里给我发消息