/** * 登录方法 * 这部分相对简单,登录以前判断XMPP是否链接,登录成功将用户状态改成在线 * @param account * @param password * @return */ public static boolean login(Context context, GeneralChatRoomHandler mhandler, CharRoomCore successful, String account, String password) { ChatRoomInfo.account = account; try { LogUtil.w("login is error"); if (XmppTool.getInstance().getConnection(context, mhandler, successful) == null) {// 若是网络没有链接上 LogUtil.i("没有网络i"); return false; } /** 登陆 * SASL的认证方式 在客户端要声明,客户端必须支持PLAIN模式--用户名和密码经过网络传输,不安全 * 关于SASL认证,有兴趣的能够查询下其余资料 * */ SASLAuthentication.supportSASLMechanism("PLAIN", 0); XmppTool.getInstance().getConnection(context, mhandler, successful).login(account, password); // 设置登陆状态:在线 Presence presence = new Presence(Presence.Type.available); XmppTool.getInstance().getConnection(context, mhandler, successful).sendPacket(presence); return true; } catch (Exception e) { e.printStackTrace(); LogUtil.w("login close"); //登陆失败记得将链接关闭 XmppTool.getInstance().closeConnection(); } return false; }
登录部分上面的注释已经写的很清楚,但我也提一点,登录操做是异步的,实际使用还须要将其放到线程里;html
关于如何创建链接请参考:http://www.cnblogs.com/ZhangXiangQian/p/5254840.html安全