openfire+smack添加好友申请及好友对申请响应监听

openfire+smack添加好友申请及好友对申请响应监听

标签: openfiresmackPresenceRoster
  2412人阅读  评论(1)  收藏  举报
  分类:

Openfire添加好友申请通过发送Presence包实现,其中Presence包的Type含有七个状态:subscribe订阅、subscribed同意订阅、unsubscribe取消订阅、unsubscribed拒绝订阅、unavailable下线、probe探测、error错误;

PacketFiler过滤器,主要用于过滤出Presence包;

PacketListener监听器,监听服务器发来的消息;

Roster对象,可以理解为保存好友的花名册,接收好友请求的三种模式:Roster.SubscriptionMode.accept_all接受所有、Roster.SubscriptionMode.reject_all拒绝所有、Roster.SubscriptionMode.manual手动处理所有;

Openfire的ofroster表的字段包括sub订阅、ask是否有发送订阅请求、rec是否有接受订阅请求;

sub订阅:

-1  remove  发送删除用户请求

0    none     用户没有建立好友关系

1    to          发送订阅请求且请求被接受

2    from      接受好友订阅请求

3    both      双方互为好友关系

ask是否有发送订阅请求:

-1    null      没有发送好友请求

0    subscribe  发送好友订阅请求但没回复

1    unsubscribe  发送取消订阅好友请求

rec是否有接受订阅请求:

-1    null      没有收到好友订阅请求

1     sub      收到好友订阅请求但没回复

2    unsub   收到好友取消订阅请求

下面可以对照ofroster表看两个例子,好友状态就容易理解了:


openfireuser和ericfantastic这两个用户,ID32:openfireuser对于ericfantastic而言其状态为to,发送订阅请求并且已经被接受,此时ericfantastic可以接收到openfireuser的所有信息包括上线、下线、发送的消息等;ID33:ericfantastic对于openfireuser而且其状态为from,接受好友的订阅请求,对方可以收到你的状态消息,但是你无法收到对方的,必须主动添加才可以。


aaa和hhh这两个用户,sub为3,则已经互为好友,双方都能收到对方的订阅消息。

其他的状态就不一一列举了,对照着字段状态,也是很容易理解的。



贴一个自己做的添加好友及处理请求的Demo,全都在一个AddFriendActivity中实现。

          

      

布局文件addfriend.xml

[html]  view plain  copy
 print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.     <LinearLayout   
  7.         android:layout_width="fill_parent"  
  8.         android:layout_height="wrap_content"  
  9.         android:layout_marginTop="20dp"  
  10.         android:background="@drawable/linearlayout_boder"  
  11.         >  
  12.         <LinearLayout   
  13.             android:layout_width="fill_parent"  
  14.             android:layout_height="wrap_content"  
  15.             android:layout_marginTop="5dp"  
  16.             android:layout_marginBottom="5dp"  
  17.             android:orientation="horizontal"   
  18.             android:background="#FFF"  
  19.             >  
  20.             <ImageView   
  21.                 android:layout_width="40dp"  
  22.                 android:layout_height="40dp"  
  23.                 android:layout_marginLeft="5dp"  
  24.                 android:layout_gravity="center_vertical"  
  25.                 android:background="@drawable/action_search"  
  26.                 />  
  27.             <EditText   
  28.                 android:id="@+id/edit_addfriend"  
  29.                 android:layout_width="wrap_content"  
  30.                 android:layout_height="wrap_content"  
  31.                 android:layout_gravity="center_vertical"  
  32.                 android:gravity="center_vertical"  
  33.                 android:layout_marginLeft="5dp"  
  34.                 android:hint="猫友号/手机号/邮箱"  
  35.                 />    
  36.              <Button   
  37.                  android:id="@+id/btn_searchfriend"  
  38.                  android:layout_width="wrap_content"  
  39.                  android:layout_height="40dp"  
  40.                  android:layout_marginLeft="5dp"  
  41.                  android:layout_gravity="center_vertical"  
  42.                  android:background="@drawable/buttonbg"  
  43.                  android:textColor="#FFF"  
  44.                  android:text="搜索"/>  
  45.         </LinearLayout>  
  46.     </LinearLayout>  
  47.       
  48.       
  49.      <LinearLayout  
  50.         android:id="@+id/tab41"  
  51.         android:layout_width="fill_parent"  
  52.         android:layout_height="80dp"  
  53.         android:layout_marginTop="50dp"  
  54.         android:background="@drawable/linearlayout_boder"  
  55.         android:orientation="horizontal" >  
  56.   
  57.         <ImageView  
  58.             android:id="@+id/img_seachFriend"  
  59.             android:layout_width="70dp"  
  60.             android:layout_height="70dp"  
  61.             android:layout_gravity="center_vertical"  
  62.             android:layout_marginLeft="10dp"  
  63.             />  
  64.             <LinearLayout   
  65.                 android:layout_width="0dp"  
  66.                 android:layout_height="wrap_content"  
  67.                 android:layout_weight="0.95"  
  68.                 android:layout_marginLeft="5dp"  
  69.                 android:orientation="vertical"  
  70.                 android:layout_gravity="center_vertical"  
  71.                 android:gravity="center_vertical"  
  72.                 >  
  73.                   
  74.                 <TextView  
  75.                 android:id="@+id/text_searchFriend"  
  76.                 android:layout_width="wrap_content"  
  77.                 android:layout_height="30dp"  
  78.                 android:layout_gravity="center_vertical"  
  79.                 android:gravity="center_vertical"  
  80.                 android:textStyle="bold"   
  81.                 android:textSize="17sp"/>  
  82.                   
  83.                  <TextView  
  84.                 android:id="@+id/text_response"  
  85.                 android:layout_width="wrap_content"  
  86.                 android:textSize="10sp"  
  87.                 android:layout_height="30dp"  
  88.                 android:layout_gravity="center_vertical"  
  89.                 android:gravity="center_vertical"  
  90.                 />  
  91.             </LinearLayout>  
  92.               
  93.         <ImageView  
  94.             android:layout_width="wrap_content"  
  95.             android:layout_height="wrap_content"  
  96.             android:layout_gravity="center_vertical|right"  
  97.             android:background="@drawable/erwei"  
  98.             android:paddingTop="10dp" />  
  99.   
  100.         <ImageView  
  101.             android:id="@+id/img_addFriend"  
  102.             android:layout_width="wrap_content"  
  103.             android:layout_height="wrap_content"  
  104.             android:layout_gravity="center_vertical"  
  105.             android:background="@drawable/action_add"  
  106.             android:paddingTop="10dp" />  
  107.         </LinearLayout>  
  108. </LinearLayout>  

添加好友界面AddFriendActivity.java

[java]  view plain  copy
 print ?
  1. package com.example.eric_jqm_chat;  
  2.   
  3.   
  4.   
  5. import org.jivesoftware.smack.PacketListener;  
  6. import org.jivesoftware.smack.Roster;  
  7. import org.jivesoftware.smack.XMPPConnection;  
  8. import org.jivesoftware.smack.XMPPException;  
  9. import org.jivesoftware.smack.filter.AndFilter;  
  10. import org.jivesoftware.smack.filter.PacketFilter;  
  11. import org.jivesoftware.smack.filter.PacketTypeFilter;  
  12. import org.jivesoftware.smack.packet.Packet;  
  13. import org.jivesoftware.smack.packet.Presence;  
  14.   
  15.   
  16. import android.app.Activity;  
  17. import android.app.AlertDialog;  
  18. import android.app.AlertDialog.Builder;  
  19. import android.app.ProgressDialog;  
  20. import android.content.BroadcastReceiver;  
  21. import android.content.Context;  
  22. import android.content.DialogInterface;  
  23. import android.content.Intent;  
  24. import android.content.IntentFilter;  
  25. import android.graphics.drawable.Drawable;  
  26. import android.os.Bundle;  
  27. import android.os.Handler;  
  28. import android.os.Message;  
  29. import android.view.View;  
  30. import android.view.View.OnClickListener;  
  31. import android.widget.Button;  
  32. import android.widget.EditText;  
  33. import android.widget.ImageView;  
  34. import android.widget.TextView;  
  35.   
  36. /* 
  37.  *@author  Eric 
  38.  *@2015-9-7上午9:28:52 
  39.  */  
  40. public class AddFriendActivity extends Activity {  
  41.     private EditText edit_addfriend;  
  42.     private Button btn_searchfriend;  
  43.     private String name,password,response,acceptAdd,alertName,alertSubName;  
  44.     private ImageView img_searchFriend,img_addFriend;  
  45.     private TextView text_searchFriend,text_response;  
  46.     private Roster roster;  
  47.     private XMPPConnection con = ConnectServer.ConnectServer();  
  48.     private static ProgressDialog dialog;  
  49.     private AddFriendHandler handler;  
  50.     private MyReceiver receiver;  
  51.       
  52.     @Override  
  53.     protected void onCreate(Bundle savedInstanceState) {  
  54.         super.onCreate(savedInstanceState);  
  55.         setContentView(R.layout.addfriend);  
  56.           
  57.         edit_addfriend = (EditText) findViewById(R.id.edit_addfriend);  
  58.         btn_searchfriend = (Button) findViewById(R.id.btn_searchfriend);  
  59.         img_searchFriend = (ImageView) findViewById(R.id.img_seachFriend);  
  60.         img_addFriend = (ImageView) findViewById(R.id.img_addFriend);  
  61.         text_searchFriend = (TextView) findViewById(R.id.text_searchFriend);  
  62.         text_response = (TextView) findViewById(R.id.text_response);  
  63.           
  64.         name = getIntent().getStringExtra("name");  
  65.         password = getIntent().getStringExtra("password");  
  66.           
  67.         roster = con.getRoster();  
  68.         roster.setSubscriptionMode(Roster.SubscriptionMode.manual);  
  69.         try {  
  70.             con.login(name, password);  
  71.         } catch (XMPPException e) {  
  72.             e.printStackTrace();  
  73.         }  
  74.           
  75.         //注册广播  
  76.         receiver = new MyReceiver();    
  77.         IntentFilter intentFilter = new IntentFilter();    
  78.         intentFilter.addAction("com.example.eric_jqm_chat.AddFriendActivity");    
  79.         registerReceiver(receiver, intentFilter);  
  80.           
  81.           
  82.         if(con!=null&&con.isConnected()&&con.isAuthenticated()){  
  83.               
  84.         //条件过滤器  
  85.         PacketFilter filter = new AndFilter(new PacketTypeFilter(Presence.class));  
  86.         //packet监听器  
  87.         PacketListener listener = new PacketListener() {  
  88.               
  89.             @Override  
  90.             public void processPacket(Packet packet) {  
  91.                 System.out.println("PresenceService-"+packet.toXML());  
  92.                 if(packet instanceof Presence){  
  93.                     Presence presence = (Presence)packet;  
  94.                      String from = presence.getFrom();//发送方    
  95.                      String to = presence.getTo();//接收方    
  96.                      if (presence.getType().equals(Presence.Type.subscribe)) {    
  97.                          System.out.println("收到添加请求!");  
  98.                          //发送广播传递发送方的JIDfrom及字符串  
  99.                         acceptAdd = "收到添加请求!";  
  100.                         Intent intent = new Intent();  
  101.                         intent.putExtra("fromName", from);  
  102.                         intent.putExtra("acceptAdd", acceptAdd);  
  103.                         intent.setAction("com.example.eric_jqm_chat.AddFriendActivity");  
  104.                         sendBroadcast(intent);   
  105.                      } else if (presence.getType().equals(    
  106.                              Presence.Type.subscribed)) {  
  107.                         //发送广播传递response字符串   
  108.                         response = "恭喜,对方同意添加好友!";  
  109.                         Intent intent = new Intent();  
  110.                         intent.putExtra("response", response);  
  111.                         intent.setAction("com.example.eric_jqm_chat.AddFriendActivity");  
  112.                         sendBroadcast(intent);   
  113.                      } else if (presence.getType().equals(    
  114.                              Presence.Type.unsubscribe)) {  
  115.                         //发送广播传递response字符串    
  116.                         response = "抱歉,对方拒绝添加好友,将你从好友列表移除!";  
  117.                         Intent intent = new Intent();  
  118.                         intent.putExtra("response", response);  
  119.                         intent.setAction("com.example.eric_jqm_chat.AddFriendActivity");  
  120.                         sendBroadcast(intent);   
  121.                      } else if (presence.getType().equals(    
  122.                              Presence.Type.unsubscribed)){  
  123.                      } else if (presence.getType().equals(    
  124.                              Presence.Type.unavailable)) {  
  125.                          System.out.println("好友下线!");  
  126.                      } else {    
  127.                          System.out.println("好友上线!");  
  128.                      }    
  129.                 }  
  130.             }  
  131.         };  
  132.         //添加监听  
  133.         con.addPacketListener(listener, filter);  
  134.       }  
  135.           
  136.           
  137.         btn_searchfriend.setOnClickListener(new OnClickListener() {  
  138.               
  139.             @Override  
  140.             public void onClick(View arg0) {  
  141.                 //从服务器查询用户头像,并没有进行搜索,自行修改  
  142.                 Drawable drawable = new Login()  
  143.                 .getUserImage(con, edit_addfriend.getText().toString());  
  144.                 if(drawable != null){  
  145.                     img_searchFriend.setImageDrawable(drawable);  
  146.                     text_searchFriend.setText(edit_addfriend.getText().toString());  
  147.                 }else{  
  148.                     text_searchFriend.setText("抱歉,未找到该用户");  
  149.                 }  
  150.                   
  151.             }  
  152.         });  
  153.           
  154.         img_addFriend.setOnClickListener(new OnClickListener() {  
  155.               
  156.             @Override  
  157.             public void onClick(View arg0) {  
  158.                   
  159.                 if(dialog == null){  
  160.                     dialog = new ProgressDialog(AddFriendActivity.this);  
  161.                 }  
  162.                 dialog.setTitle("请等待");  
  163.                 dialog.setMessage("正在发送好友申请...");  
  164.                 dialog.setCancelable(true);  
  165.                 dialog.show();  
  166.                 //添加好友的副线程  
  167.                 Thread thread = new Thread(new Runnable() {  
  168.                     @Override  
  169.                     public void run() {  
  170.                         try {  
  171.                             Thread.sleep(1000);  
  172.                             String friendName = edit_addfriend.getText().toString();   
  173.                             boolean result = addFriend(roster, friendName, friendName);  
  174.                             Message msg = new Message();  
  175.                             Bundle b = new Bundle();  
  176.                             b.putBoolean("result", result);  
  177.                             msg.setData(b);  
  178.                             handler.sendMessage(msg);  
  179.                         } catch (Exception e) {  
  180.                             System.out.println("申请发生异常!!");  
  181.                             e.printStackTrace();  
  182.                         }  
  183.                     }  
  184.                 });  
  185.                 //启动线程和实例化handler  
  186.                 thread.start();  
  187.                 handler = new AddFriendHandler();  
  188.                   
  189.             }  
  190.         });  
  191.           
  192.     }  
  193.       
  194.     //handler更新UI线程  
  195.     public class AddFriendHandler extends Handler{  
  196.         @Override  
  197.         public void handleMessage(Message msg) {  
  198.               
  199.             if(dialog != null){  
  200.                 dialog.setMessage("发送成功!");  
  201.                 dialog.dismiss();  
  202.             }  
  203.             Bundle b = msg.getData();  
  204.             Boolean res = b.getBoolean("result");  
  205.             if (res == true){  
  206.                 System.out.println("button发送添加好友请求成功!!");  
  207.             }  
  208.               
  209.               
  210.         }  
  211.     }  
  212.       
  213.      //添加好友  
  214.     public  boolean addFriend(Roster roster,String friendName,String name){  
  215.           
  216.         try {  
  217.             roster.createEntry(friendName.trim()+"@eric-pc", name, new String[]{"Friends"});  
  218.             System.out.println("添加好友成功!!");  
  219.             return true;  
  220.         } catch (XMPPException e) {  
  221.             e.printStackTrace();  
  222.               
  223.             System.out.println("失败!!"+e);  
  224.             return false;  
  225.         }  
  226.     }  
  227.       
  228.       
  229.     //广播接收器  
  230.     public class MyReceiver extends BroadcastReceiver{  
  231.   
  232.         @Override  
  233.         public void onReceive(Context context, Intent intent) {  
  234.             //接收传递的字符串response  
  235.             Bundle bundle = intent.getExtras();  
  236.             response = bundle.getString("response");  
  237.             System.out.println("广播收到"+response);  
  238.             text_response.setText(response);  
  239.             if(response==null){  
  240.                 //获取传递的字符串及发送方JID  
  241.                 acceptAdd = bundle.getString("acceptAdd");  
  242.                 alertName = bundle.getString("fromName");  
  243.                 if(alertName!=null){  
  244.                     //裁剪JID得到对方用户名  
  245.                     alertSubName = alertName.substring(0,alertName.indexOf("@"));  
  246.                 }  
  247.                 if(acceptAdd.equals("收到添加请求!")){  
  248.                     //弹出一个对话框,包含同意和拒绝按钮  
  249.                      AlertDialog.Builder builder  = new Builder(AddFriendActivity.this);  
  250.                      builder.setTitle("添加好友请求" ) ;  
  251.                      builder.setMessage("用户"+alertSubName+"请求添加你为好友" ) ;  
  252.                      builder.setPositiveButton("同意",new DialogInterface.OnClickListener() {  
  253.                             //同意按钮监听事件,发送同意Presence包及添加对方为好友的申请  
  254.                             @Override  
  255.                             public void onClick(DialogInterface dialog, int arg1) {  
  256.                                 Presence presenceRes = new Presence(Presence.Type.subscribed);  
  257.                                 presenceRes.setTo(alertName);  
  258.                                 con.sendPacket(presenceRes);  
  259.                                   
  260.                                 addFriend(roster, alertSubName, alertSubName);  
  261.                             }  
  262.                         });  
  263.                      builder.setNegativeButton("拒绝"new DialogInterface.OnClickListener() {  
  264.                         //拒绝按钮监听事件,发送拒绝Presence包  
  265.                         @Override  
  266.                         public void onClick(DialogInterface dialog, int arg1) {  
  267.                             Presence presenceRes = new Presence(Presence.Type.unsubscribe);  
  268.                             presenceRes.setTo(alertName);  
  269.                             con.sendPacket(presenceRes);  
  270.                         }  
  271.                     });  
  272.                      builder.show();  
  273.                 }  
  274.             }  
  275.               
  276.         }  
  277.           
  278.     }  
  279.       
  280.      
  281.       
  282. }  
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值