private void initState() { getSupportActionBar().setTitle(XMPPManager.getInstance().getState().getMessage()); chatManager = ChatManager.getInstanceFor(XMPPManager.getInstance().connection); chatManager.addChatListener(new ChatManagerListener() { @Override public void chatCreated(Chat chat, boolean createdLocally) { if (createdLocally) { // Chat created by myself } else { // Chat created by others chat.addMessageListener(chatMessageListener); } } }); if (!TextUtils.isEmpty(username)) { chat = chatManager.createChat(username + "@" + XMPPManager.serverName, chatMessageListener); } }
/** * 初始化XMPP */ void initXMPP() { mXMPPConnection = mXmppConnectionManager.init(); //初始化XMPPConnection loginXMPP(); //登录XMPP ChatManager chatmanager = mXMPPConnection.getChatManager(); chatmanager.addChatListener(new ChatManagerListener() { @Override public void chatCreated(Chat arg0, boolean arg1) { arg0.addMessageListener(new MsgListener(MsfService.this, mNotificationManager)); } }); }
public void connect() { try { ConnectionConfiguration config = new ConnectionConfiguration(server, 5222); config.setCompressionEnabled(true); config.setSASLAuthenticationEnabled(true); config.setReconnectionAllowed(true); connection = new XMPPConnection(config); connection.connect(); connection.login(username, password); final ChatManager chatManager = connection.getChatManager(); chatManager.addChatListener(new ChatManagerListener() { @Override public void chatCreated(Chat chat, boolean createdLocally) { chat.addMessageListener(getMessageListener()); } }); } catch (XMPPException e) { throw new RuntimeException(e); } }