Android smack


  登陆:

  

if (!ConnectionUtils.getConnection().isConnected())             
     return false;
if (ConnectionUtils.getConnection().isAuthenticated())
 {
 return true;
} 
else 
{
// 用户登录
ConnectionUtils.getConnection().login("username","password");
 return ConnectionUtils.getConnection().isAuthenticated();
} 

 

 读取消息:

 PacketListener和PacketCollector。

   都使用PacketFilter

 

 PacketFilter packetFilter = new AndFilter(new PacketIDFilter(jp.getPacketID()), new PacketTypeFilter(IQ.class));
            // 监听所有的包
            con.addPacketListener(new PacketListener() {
                public void processPacket(Packet packet) {
                    if (packet instanceof MyIQ) {
                        MyIQ iq = (MyIQ) packet;
                        if (iq.getType() == IQ.Type.RESULT) {
                            System.out.println("返回内容:"+ iq.toXML()); 
                        }
                       } 
                    else {
                        System.out.println("其他消息:"+packet.toXML());
                       }
                    }
            }, packetFilter);
         //PacketCollector
    PacketFilter filter = new AndFilter(new PacketIDFilter(reg.getPacketID()), new PacketTypeFilter(IQ.class));
    PacketCollector collector = ConnectionUtils.getConnection().createPacketCollector(filter);
    ConnectionUtils.getConnection().sendPacket(reg);
    result = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());

  

 添加好友

    在申请添加一个好友,当对方Roster.setDefaultSubscriptionMode(SubscriptionMode.manual)时,此时添加的好友为“挂起”状态。

    对方设置监听


  

PacketFilter filter = new PacketFilter() {

 @Override
public boolean accept(Packet packet) {
if (packet instanceof Presence) {
 Presence presence = (Presence) packet;
if (presence.getType().equals(Presence.Type.subscribe)) {
return true;
 }
 }
return false;
 }
 };

 允许添加好友则回复消息


 

Presence presence = new Presence(Presence.Type.subscribed);
 presence.setTo(packet.getFrom());
 ConnectionUtils.getConnection().sendPacket(presence);
 此时邀请人的好友状态为to。

 而被添加的user的subscription状态为from。

如果要改为both。

被邀请人应当也发送一个邀请请求。


Presence subscription = new Presence(Presence.Type.subscribe);
subscription.setTo(packet.getFrom());
ConnectionUtils.getConnection().sendPacket(subscription);


 申请创建联系人 : createEntry(String user, String name, String[] groups)
 删除联系人 : removeEntry(RosterEntry entry)

   

      .... 后续

 


你可能感兴趣的:(Android smack)