2015年1月25日 星期日

Android 基於xmpp協議,smack包,openfire服務端的高仿QQ的即時通訊實現【4】監聽別人...

http://www.apkbus.com/blog-125132-52803.html

先介绍一个方法类,主要是添加好友等方法。
[html] view plaincopy
  1. public class XmppService{  
  2.   
  3.       
  4.     /**   
  5.      * 删除当前用户   
  6.      * @param connection   
  7.      * @return   
  8.      */    
  9.     public static boolean deleteAccount(XMPPConnection connection)    
  10.     {    
  11.         try {    
  12.             connection.getAccountManager().deleteAccount();           
  13.             return true;    
  14.         } catch (Exception e) {    
  15.             return false;    
  16.         }    
  17.     }    
  18.     /**  
  19.      * 返回所有组信息 <RosterGroup>  
  20.      * @return List(RosterGroup)  
  21.      */  
  22.     public static List<RosterGroup> getGroups(Roster roster) {  
  23.         List<RosterGroup> groupsList = new ArrayList<RosterGroup>();  
  24.         Collection<RosterGroup> rosterGroup = roster.getGroups();  
  25.         Iterator<RosterGroup> i = rosterGroup.iterator();  
  26.         while (i.hasNext())  
  27.             groupsList.add(i.next());  
  28.         return groupsList;  
  29.     }  
  30.   
  31.     /**  
  32.      * 返回相应(groupName)组里的所有用户<RosterEntry>  
  33.      * @return List(RosterEntry)  
  34.      */  
  35.     public static List<RosterEntry> getEntriesByGroup(Roster roster,  
  36.             String groupName) {  
  37.         List<RosterEntry> EntriesList = new ArrayList<RosterEntry>();  
  38.         RosterGroup rosterGroup = roster.getGroup(groupName);  
  39.         Collection<RosterEntry> rosterEntry = rosterGroup.getEntries();  
  40.         Iterator<RosterEntry> i = rosterEntry.iterator();  
  41.         while (i.hasNext())  
  42.             EntriesList.add(i.next());  
  43.         return EntriesList;  
  44.     }  
  45.   
  46.     /**  
  47.      * 返回所有用户信息 <RosterEntry>  
  48.      * @return List(RosterEntry)  
  49.      */  
  50.     public static List<RosterEntry> getAllEntries(Roster roster) {  
  51.         List<RosterEntry> EntriesList = new ArrayList<RosterEntry>();  
  52.         Collection<RosterEntry> rosterEntry = roster.getEntries();  
  53.         Iterator<RosterEntry> i = rosterEntry.iterator();  
  54.         while (i.hasNext())  
  55.             EntriesList.add(i.next());  
  56.         return EntriesList;  
  57.     }  
  58.       
  59.       
  60.     /**   
  61.      * 创建一个组   
  62.      */   
  63.     public static boolean addGroup(Roster roster,String groupName)    
  64.     {    
  65.         try {    
  66.             roster.createGroup(groupName);    
  67.             return true;    
  68.         } catch (Exception e) {    
  69.             e.printStackTrace();    
  70.             return false;    
  71.         }    
  72.     }    
  73.         
  74.     /**   
  75.      * 删除一个组   
  76.      */    
  77.     public static boolean removeGroup(Roster roster,String groupName)    
  78.     {    
  79.         return false;    
  80.     }  
  81.       
  82.     /**  
  83.      * 添加一个好友  无分组  
  84.      */  
  85.     public static boolean addUser(Roster roster,String userName,String name)  
  86.     {  
  87.         try {  
  88.             roster.createEntry(userName, name, null);  
  89.             return true;  
  90.         } catch (Exception e) {  
  91.             e.printStackTrace();  
  92.             return false;  
  93.         }  
  94.           
  95.     }  
  96.     /**  
  97.      * 添加一个好友到分组  
  98.      * @param roster  
  99.      * @param userName  
  100.      * @param name  
  101.      * @return  
  102.      */  
  103.     public static boolean addUsers(Roster roster,String userName,String name,String groupName)  
  104.     {  
  105.         try {  
  106.             roster.createEntry(userName, name,new String[]{ groupName});  
  107.             return true;  
  108.         } catch (Exception e) {  
  109.             e.printStackTrace();  
  110.             return false;  
  111.         }  
  112.           
  113.     }  
  114.       
  115.     /**  
  116.      * 删除一个好友  
  117.      * @param roster  
  118.      * @param userJid  
  119.      * @return  
  120.      */  
  121.     public static boolean removeUser(Roster roster,String userJid)  
  122.     {  
  123.         try {  
  124.             RosterEntry entry = roster.getEntry(userJid);  
  125.             roster.removeEntry(entry);  
  126.             return true;  
  127.         } catch (Exception e) {  
  128.             e.printStackTrace();  
  129.             return false;  
  130.         }         
  131.     }  
  132.       
  133.     /**  
  134.      * 把一个好友添加到一个组中  
  135.      * @param userJid  
  136.      * @param groupName  
  137.      */  
  138.     public static void addUserToGroup(final String userJid, final String groupName,  
  139.             final XMPPConnection connection) {  
  140.                 RosterGroup group = connection.getRoster().getGroup(groupName);  
  141.                 // 这个组已经存在就添加到这个组,不存在创建一个组  
  142.                 RosterEntry entry = connection.getRoster().getEntry(userJid);  
  143.                 try {  
  144.                     if (group != null) {  
  145.                         if (entry != null)  
  146.                             group.addEntry(entry);  
  147.                     } else {  
  148.                         RosterGroup newGroup = connection.getRoster().createGroup("我的好友");  
  149.                         if (entry != null)  
  150.                             newGroup.addEntry(entry);  
  151.                     }  
  152.                 } catch (Exception e) {  
  153.                     e.printStackTrace();  
  154.                 }  
  155.     }  
  156.   
  157.     /**  
  158.      * 把一个好友从组中删除  
  159.      * @param userJid  
  160.      * @param groupName  
  161.      */  
  162.     public static void removeUserFromGroup(final String userJid,final String groupName, final XMPPConnection connection) {  
  163.             RosterGroup group = connection.getRoster().getGroup(groupName);  
  164.             if (group != null) {  
  165.                 try {  
  166.                     RosterEntry entry = connection.getRoster().getEntry(userJid);  
  167.                     if (entry != null)  
  168.                         group.removeEntry(entry);  
  169.                 } catch (XMPPException e) {  
  170.                     e.printStackTrace();  
  171.                 }  
  172.             }  
  173.      }  
  174.       
  175.     /**   
  176.      * 修改心情   
  177.      * @param connection   
  178.      * @param status   
  179.      */    
  180.     public static void changeStateMessage(final XMPPConnection connection,final String status)    
  181.     {    
  182.         Presence presence = new Presence(Presence.Type.available);    
  183.         presence.setStatus(status);    
  184.         connection.sendPacket(presence);        
  185.     }    
  186. }  
当对方发送好友申请的时候,主要监听代码:
[html] view plaincopy
  1. roster.addRosterListener(    
  2.              new RosterListener() {     
  3.                     @Override    
  4.                     //监听好友申请消息  
  5.                     public void entriesAdded(Collection<String> invites) {    
  6.                         // TODO Auto-generated method stub    
  7.                         System.out.println("监听到好友申请的消息是:"+invites);   
  8.                         for (Iterator iter = invites.iterator(); iter.hasNext();) {  
  9.                               String fromUserJids = (String)iter.next();  
  10.                               System.out.println("fromUserJids是:"+fromUserJids);  
  11.                               fromUserJid = fromUserJids;  
  12.                         }                      
  13.                          if(fromUserJid!=null){  
  14.                            Intent intent = new Intent();  
  15.                            intent.putExtra("USERID", pUSERID);  
  16.                            intent.putExtra("fromUserJid", fromUserJid);  
  17.                            intent.setClass(FriendListActivity.this, FriendListActivity.class);  
  18.                            startActivity(intent);   
  19.                        }       
  20.                     }      
  21.                     @Override    
  22.                     //监听好友同意添加消息  
  23.                     public void entriesUpdated(Collection<String> invites) {    
  24.                             // TODO Auto-generated method stub    
  25.                            System.out.println("监听到好友同意的消息是:"+invites);    
  26.                            for (Iterator iter = invites.iterator(); iter.hasNext();) {  
  27.                               String fromUserJids = (String)iter.next();  
  28.                               System.out.println("同意添加的好友是:"+fromUserJids);  
  29.                               toUserJid = fromUserJids;  
  30.                             }   
  31.                             if(toUserJid!=null){  
  32.                                 XmppService.addUserToGroup(toUserJid, pGROUPNAME, connection);  
  33.                                 loadFriend();  
  34.                             }                         
  35.                     }   
  36.                     @Override    
  37.                     //监听好友删除消息  
  38.                     public void entriesDeleted(Collection<String> delFriends) {    
  39.                         // TODO Auto-generated method stub    
  40.                         System.out.println("监听到删除好友的消息是:"+delFriends);    
  41.                         if(delFriends.size()>0){  
  42.                             loadFriend();  
  43.                         }    
  44.                     }   
  45.                    @Override    
  46.                    //监听好友状态改变消息  
  47.                     public void presenceChanged(Presence presence) {    
  48.                         // TODO Auto-generated method stub    
  49.                         friendMood = presence.getStatus();  
  50.                         System.out.println("presence.getStatus()是:"+presence.getStatus());  
  51.             }       
  52.                             
  53.         });    

删除好友的设计:

主要代码:
[html] view plaincopy
  1. /**  
  2.      * 长按事件删除好友  
  3.      */  
  4.     @Override  
  5.     public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {  
  6.         super.onCreateContextMenu(menu, v, menuInfo);  
  7.         if(menuInfo instanceof ExpandableListView.ExpandableListContextMenuInfo){  
  8.               
  9.             ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo) menuInfo;  
  10.               
  11.             int type = ExpandableListView.getPackedPositionType(info.packedPosition);  
  12.               
  13.             if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {  
  14.                   
  15.                 int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);  
  16.                 int childPos = ExpandableListView.getPackedPositionChild(info.packedPosition);  
  17.                 final FriendInfo dInfo = groupList.get(groupPos).getFriendInfoList().get(childPos);  
  18.                 final GroupInfo  gInfo = groupList.get(groupPos);  
  19.                 LayoutInflater layoutInflaterLayoutInflater.from(this);  
  20.                 View delFriendView = layoutInflater.inflate(R.layout.dialog_del_friend, null);  
  21.                 TextView delname = (TextView)delFriendView.findViewById(R.id.delname);  
  22.                 delname.setText(dInfo.getJid());  
  23.                 final CheckBox delCheckBox = (CheckBox)delFriendView.findViewById(R.id.delCheckBox);  
  24.                 Dialog dialog =new AlertDialog.Builder(this)  
  25.                 .setIcon(R.drawable.default_head)  
  26.                 .setTitle("删除好友")  
  27.                 .setView(delFriendView)  
  28.                 .setPositiveButton("确定", new DialogInterface.OnClickListener(){  
  29.                     @Override  
  30.                     public void onClick(DialogInterface dialog, int which){  
  31.                          XmppService.removeUserFromGroup(dInfo.getJid(), gInfo.getGroupName(), connection);  
  32.                          if(delCheckBox.isChecked()){  
  33.                              XmppService.removeUser(roster, dInfo.getJid());  
  34.                          }  
  35.                          Intent intent = new Intent();  
  36.                          intent.putExtra("USERID", pUSERID);  
  37.                          intent.putExtra("fromUserJid", CHECK);  
  38.                          intent.setClass(FriendListActivity.this, FriendListActivity.class);  
  39.                          startActivity(intent);   
  40.                     }  
  41.                 })  
  42.                 .setNegativeButton("取消", new DialogInterface.OnClickListener(){  
  43.                     @Override  
  44.                     public void onClick(DialogInterface dialog, int which){  
  45.                             dialog.cancel();  
  46.                         }  
  47.                 })  
  48.                 .create();  
  49.                 dialog.show();  
  50.             }  
  51.         }  
  52.     }