2012-03-18 76 views
1

实际上,问题是当我的xmpp客户端发送好友邀请,然后收件人已批准邀请时,openfire服务器再次向发起者/邀请发送者推送要授权的订阅数据包,这就是为什么我要防止这种情况通过使用IQ标签自动进行过滤,然后自动对其进行授权。如何使用smack获得IQ标签?

但PacketListener,我不能让智商的标签......

我怎样才能做到这一点?

@Override 
public void processPacket(Packet packet) { 
    Log.i(TAG, "SECOND subscription"); 
    Log.d(TAG, "SECOND: "+packet.toXML()); 
    if (packet instanceof Presence) { 
     Presence p = (Presence) packet; 
     Log.d(TAG, "TYPE-Presence: "+p.getType()); 
     if (p.getType() != Presence.Type.subscribe) 
     return; 
     String from = p.getFrom(); 
     Log.d(TAG, "PACKET from: "+from); 
     Notification notification = new Notification(android.R.drawable.stat_notify_more, mService.getString(
       R.string.AcceptContactRequest, from), System.currentTimeMillis()); 
     notification.flags = Notification.FLAG_AUTO_CANCEL; 
     Intent intent = new Intent(mService, Subscription.class); 
     intent.setData(Contact.makeXmppUri(from)); 
     notification.setLatestEventInfo(mService, from, mService 
       .getString(R.string.AcceptContactRequestFrom, from), PendingIntent.getActivity(mService, 0, 
         intent, PendingIntent.FLAG_ONE_SHOT)); 
     int id = p.hashCode(); 
     mService.sendNotification(id, notification); 
    } 
} 

回答

3

可以通过使用“IQTypeFilter”过滤器过滤输入的IQ。这是说明该方法的示例代码。

connection.connect(); 

    /* packet listener: listen for incoming messages of type IQ on the connection (whatever the buddy) */ 
    PacketFilter filter = new IQTypeFilter(IQ.Type.SET); // or IQ.Type.GET etc. according to what you like to filter. 

    connection.addPacketListener(new PacketListener() { 
     public void processPacket(Packet packet) { 
      // HERE YOU PUT YOUR CODE TO HANDLE THE IQ MESSAGE 
     } 
    }, filter); 
+0

工作太好了!谢谢! – user724861 2012-04-08 15:12:21

+0

您能否详细说明您的答案。其实我不知道如何处理IQ消息 – 2014-03-10 11:00:12