示例#1
0
  public static void login() {
    XMPPTCPConnection connection = XmppTool.getInstance(mContext).getConnection();
    try {
      connection.connect();
    } catch (SmackException e) {
      // e.printStackTrace();
    } catch (IOException e) {
      // e.printStackTrace();
    } catch (XMPPException e) {
      // e.printStackTrace();
    }
    UserService uService = new UserService(mContext);
    Session mSession = Session.get(mContext);
    try {
      connection.login(mSession.getUid(), mSession.getPassword(), "XMPPTCPConnection");
    } catch (XMPPException e) {
      // e.printStackTrace();
    } catch (SmackException e) {
      // e.printStackTrace();
    } catch (IOException e) {
      // e.printStackTrace();
    }

    if (connection.isConnected() && connection.isAuthenticated()) {
      Presence presence = new Presence(Presence.Type.available);
      try {
        connection.sendStanza(presence);
      } catch (SmackException.NotConnectedException e) {
        e.printStackTrace();
      }
    }
  }
示例#2
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();
    }
  }
  private void logoutSession() {
    try {
      DataHolder.setLoggedUser(null);
      QBRTCClient.getInstance(this).destroy();

      // comment if you haven't started ping alarm
      ChatPingAlarmManager.onDestroy();
      QBChatService.getInstance().logout();
    } catch (SmackException.NotConnectedException e) {
      e.printStackTrace();
    }
  }
示例#4
0
 public void removeFriend(String username) {
   Roster roster = jc.getConnection().getRoster();
   RosterEntry entry = roster.getEntry(username);
   try {
     jc.getConnection().getRoster().removeEntry(entry);
   } catch (SmackException.NotLoggedInException e) {
     e.printStackTrace();
   } catch (SmackException.NoResponseException e) {
     e.printStackTrace();
   } catch (XMPPException.XMPPErrorException e) {
     e.printStackTrace();
   } catch (SmackException.NotConnectedException e) {
     e.printStackTrace();
   }
 }
示例#5
0
  public void addFriend(String username, String nickname) {
    Roster roster = jc.getConnection().getRoster();
    try {

      roster.createEntry(username, nickname, null);
      Log.i("AddFriend:", username);
    } catch (SmackException.NotLoggedInException e) {
      e.printStackTrace();
    } catch (SmackException.NoResponseException e) {
      e.printStackTrace();
    } catch (XMPPException.XMPPErrorException e) {
      e.printStackTrace();
    } catch (SmackException.NotConnectedException e) {
      e.printStackTrace();
    }
  }
示例#6
0
  private Bitmap loadProfileImageWithVCard(String username) {
    Bitmap bitmap = null;
    Log.e(TAG, "Get pic from server");
    VCard vCard = new VCard();
    try {
      vCard.load(JabberConnection.getInstance().getConnection(), username + "@pc-pc");
      byte[] bytes = vCard.getAvatar();
      bitmap = BitmapUtil.getBitmapFromBytes(bytes);

    } catch (SmackException.NoResponseException e) {
      e.printStackTrace();
    } catch (XMPPException.XMPPErrorException e) {
      e.printStackTrace();
    } catch (SmackException.NotConnectedException e) {
      e.printStackTrace();
    }
    return bitmap;
  }
示例#7
0
  /** 发送消息 */
  public void sendMessage(String sessionJID, String sessionName, String message, String type)
      throws RemoteException {
    ChatManager chatManager = ChatManager.getInstanceFor(connection);
    Chat chat;
    // 查找Chat对策
    if (jidChats.containsKey(sessionJID)) {
      chat = jidChats.get(sessionJID);
      // 创建Chat
    } else {
      chat = chatManager.createChat(sessionJID, null);
      // 添加到集合
      jidChats.put(sessionJID, chat);
    }

    if (chat != null) {
      try {
        // 发送消息
        chat.sendMessage(message);

        // 保存聊天记录
        ContentValues values = new ContentValues();
        values.put(SMSProvider.SMSColumns.BODY, message);
        values.put(SMSProvider.SMSColumns.TYPE, type);
        values.put(SMSProvider.SMSColumns.TIME, System.currentTimeMillis());

        values.put(SMSProvider.SMSColumns.WHO_ID, IM.getString(IM.ACCOUNT_JID));

        values.put(SMSProvider.SMSColumns.SESSION_ID, sessionJID);
        values.put(SMSProvider.SMSColumns.SESSION_NAME, sessionName);

        imService.getContentResolver().insert(SMSProvider.SMS_URI, values);

      } catch (XMPPException e) {
        e.printStackTrace();
      } catch (SmackException.NotConnectedException e) {
        e.printStackTrace();
      }
    }
  }