예제 #1
0
  public static void main(String[] args) {
    SmackConfiguration.DEBUG = true;
    SmackConfiguration.setDefaultPacketReplyTimeout(10 * 1000);

    XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder();
    configBuilder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
    configBuilder.setUsernameAndPassword("registeruser2", "123456");

    configBuilder.setServiceName("ejabberddemo.com");
    configBuilder.setDebuggerEnabled(true).build();

    // 必须在用户状态为离线状态
    configBuilder.setSendPresence(false);

    AbstractXMPPConnection connection = new XMPPTCPConnection(configBuilder.build());
    try {
      connection.connect();
    } catch (SmackException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (XMPPException e) {
      e.printStackTrace();
    }

    // FIXME
    OfflineMessageManager offlineManager = new OfflineMessageManager(connection);

    try {
      connection.login();
    } catch (XMPPException e) {
      e.printStackTrace();
    } catch (SmackException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }

    try {
      System.out.println(offlineManager.supportsFlexibleRetrieval());
      System.out.println(offlineManager.getMessageCount());
    } catch (SmackException.NoResponseException e) {
      e.printStackTrace();
    } catch (XMPPException.XMPPErrorException e) {
      e.printStackTrace();
    } catch (SmackException.NotConnectedException e) {
      e.printStackTrace();
    }
    // 上线
    Presence presence = new Presence(Presence.Type.available);
    try {
      connection.sendPacket(presence);
    } catch (SmackException.NotConnectedException e) {
      e.printStackTrace();
    }
  }
예제 #2
0
 /** 建立XMPP连接 */
 public boolean connect() throws RemoteException {
   // 已经连接
   if (connection.isConnected()) {
     return true;
   } else {
     try {
       // 开始连接
       connection.connect();
       if (connectionListener == null) {
         // 添加一个连接监听器
         connectionListener = new IMClientConnectListener();
       }
       connection.addConnectionListener(connectionListener);
       return true;
     } catch (SmackException e) {
       e.printStackTrace();
     } catch (IOException e) {
       e.printStackTrace();
     } catch (XMPPException e) {
       e.printStackTrace();
     }
   }
   return false;
 }
예제 #3
0
 /** 登陆XMPP服务器 */
 public boolean login() throws RemoteException {
   // 未建立XMPP连接
   if (!connection.isConnected()) {
     return false;
   }
   // 应经登陆过
   if (connection.isAuthenticated()) {
     return true;
   } else {
     // 开始登陆
     try {
       connection.login(account, password, imService.getString(R.string.app_name));
       if (messageListener == null) {
         messageListener = new MessageListener();
       }
       // 添加消息监听器
       connection.addPacketListener(messageListener, new PacketTypeFilter(Message.class));
       Roster roster = connection.getRoster();
       if (rosterListener == null) {
         rosterListener = new IMClientRosterListener();
       }
       // 添加花名册监听器
       roster.addRosterListener(rosterListener);
       // 获取花名册
       if (roster != null && roster.getEntries().size() > 0) {
         Uri uri = null;
         for (RosterEntry entry : roster.getEntries()) {
           // 获取联系人名片信息
           VCard vCard = new VCard();
           vCard.load(connection, entry.getUser());
           // 用户名称
           String userName = StringUtils.parseName(entry.getUser());
           // 用户备注
           String remarks = entry.getName();
           // 通讯录的名称
           String name = "";
           // 名称与备注判断
           if (userName.equals(remarks) && vCard != null) {
             // 使用联系人的昵称
             name = vCard.getNickName();
           } else {
             // 使用备注
             name = remarks;
           }
           if (vCard != null) {
             IM.saveAvatar(vCard.getAvatar(), StringUtils.parseName(entry.getUser()));
           }
           ContentValues values = new ContentValues();
           values.put(ContactsProvider.ContactColumns.ACCOUNT, entry.getUser());
           values.put(ContactsProvider.ContactColumns.NAME, name);
           String sortStr = PinYin.getPinYin(name);
           values.put(ContactsProvider.ContactColumns.SORT, sortStr);
           values.put(
               ContactsProvider.ContactColumns.SECTION,
               sortStr.substring(0, 1).toUpperCase(Locale.ENGLISH));
           // 储存联系人
           if (imService
                   .getContentResolver()
                   .update(
                       ContactsProvider.CONTACT_URI,
                       values,
                       ContactsProvider.ContactColumns.ACCOUNT + " = ?",
                       new String[] {entry.getUser()})
               == 0) {
             uri = imService.getContentResolver().insert(ContactsProvider.CONTACT_URI, values);
           }
         }
         // 发生改变,通知刷新
         if (uri != null) {
           imService.getContentResolver().notifyChange(uri, null);
         }
       }
     } catch (XMPPException e) {
       e.printStackTrace();
     } catch (SmackException e) {
       e.printStackTrace();
     } catch (IOException e) {
       e.printStackTrace();
     }
     return true;
   }
 }