Android基于XMPP开发(三)【注册、登录、修改密码、修改头像】 发表于 2016-03-01 | 分类于 XMPP | 阅读次数: Android基于XMPP开发(一)【openfire服务器配置】Android基于XMPP开发(二)【连接服务器】 本篇将涉及常用的账号管理,包括:注册、登录、修改密码、修改头像。 注册123456789101112131415161718192021222324252627282930313233343536373839404142public void register(final String userName, final String password) { new Thread(new Runnable() { @Override public void run() { String registerMsg; try { if (!mXMPPTCPConnection.isConnected()) mXMPPTCPConnection.connect(); } catch (Exception e) { e.printStackTrace(); mXMPPTCPConnection.disconnect(); } Log.i("wxl", "XMPPService connected=" + mXMPPTCPConnection.isConnected()); if (mXMPPTCPConnection.isConnected()) { String serviceName = mXMPPTCPConnection.getServiceName(); try { AccountManager accountManager = AccountManager.getInstance(mXMPPTCPConnection); Log.i("wxl", "Register supportsAccountCreation=" + accountManager.supportsAccountCreation()); if (accountManager.supportsAccountCreation()) { accountManager.createAccount(userName + "@" + serviceName, password); Log.i(TAG, "注册成功"); registerMsg = "注册成功"; } else { Log.i(TAG, "服务端不能注册"); registerMsg = "服务端不能注册"; } } catch (Exception e) { e.printStackTrace();// Log.i(TAG, "注册异常=" + e.getMessage()); registerMsg = "注册异常=" + e.getMessage(); } } else { Log.i(TAG, "connect failed"); registerMsg = "connect failed"; } mXMPPClickListener.register(registerMsg); } }).start(); } 登录12345678910111213141516171819202122232425262728293031323334353637383940public void login(final String userName, final String password) { new Thread(new Runnable() { @Override public void run() { String loginMsg; try { if (!mXMPPTCPConnection.isConnected()) mXMPPTCPConnection.connect(); } catch (Exception e) { e.printStackTrace(); mXMPPTCPConnection.disconnect(); } Log.i("wxl", "XMPPService login connected=" + mXMPPTCPConnection.isConnected()); if (mXMPPTCPConnection.isConnected()) { try { mXMPPTCPConnection.login(userName, password); if (mXMPPTCPConnection.isAuthenticated()) { Log.i(TAG, "登录成功"); loginMsg = "登录成功"; } else { Log.i(TAG, "登录失败"); loginMsg = "登录失败"; } } catch (Exception e) { e.printStackTrace();// Log.i(TAG, "登录异常=" + e.getMessage()); loginMsg = "登录异常=" + e.getMessage(); } } else { Log.i(TAG, "connect failed"); loginMsg = "connect failed"; } mXMPPClickListener.login(loginMsg); } }).start(); } 修改密码修改密码必须在登录状态下12345678910111213141516171819202122232425262728293031323334353637383940public void changePassword(final String newPassword) { new Thread(new Runnable() { @Override public void run() { String changePasswordMsg; try { if (!mXMPPTCPConnection.isConnected()) mXMPPTCPConnection.connect(); } catch (Exception e) { e.printStackTrace(); mXMPPTCPConnection.disconnect(); } Log.i("wxl", "XMPPService connected=" + mXMPPTCPConnection.isConnected()); if (mXMPPTCPConnection.isConnected()) { try { if (mXMPPTCPConnection.isAuthenticated()) { AccountManager accountManager = AccountManager.getInstance(mXMPPTCPConnection); accountManager.changePassword(newPassword); Log.i(TAG, "修改密码成功"); changePasswordMsg = "修改密码成功"; } else { Log.i(TAG, "请先登录"); changePasswordMsg = "请先登录"; } } catch (Exception e) { e.printStackTrace();// Log.i(TAG, "修改密码异常=" + e.getMessage()); changePasswordMsg = "修改密码异常=" + e.getMessage(); } } else { Log.i(TAG, "connect failed"); changePasswordMsg = "connect failed"; } mXMPPClickListener.register(changePasswordMsg); } }).start(); } 修改头像1234567891011121314151617181920212223242526272829303132333435363738public void setAvatar(final Bitmap bitmap) { new Thread(new Runnable() { @Override public void run() { try { if (!mXMPPTCPConnection.isConnected()) mXMPPTCPConnection.connect(); } catch (Exception e) { e.printStackTrace(); mXMPPTCPConnection.disconnect(); } Log.i("wxl", "XMPPService connected=" + mXMPPTCPConnection.isConnected()); if (mXMPPTCPConnection.isConnected()) { try { if (mXMPPTCPConnection.isAuthenticated()) { VCardManager vCardManager = VCardManager.getInstanceFor(mXMPPTCPConnection); VCard vCard = vCardManager.loadVCard(); byte[] bytes = bitmapToByte(bitmap); String encodedImage = Base64.encodeToString(bytes, Base64.DEFAULT); vCard.setAvatar(bytes); vCardManager.saveVCard(vCard); mXMPPClickListener.setAvatar("修改头像成功"); } else { mXMPPClickListener.setAvatar("请先登录"); } } catch (Exception e) { e.printStackTrace();// mXMPPClickListener.setAvatar("修改头像异常=" + e.getMessage()); } } else { mXMPPClickListener.setAvatar("connect failed"); } } }).start(); } bitmapToByte12345private byte[] bitmapToByte(Bitmap bitmap) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); return baos.toByteArray();} 源码地址https://github.com/WuXiaolong/WoChat 联系作者 我的微信公众号:吴小龙同学,欢迎关注交流,公号回复关键字「1024」有惊喜哦。