2 创建聊天室
/** * 创建多用户聊天室 */ private void createMultiUserChat() { if (!Sessions.isLoginOfSuccess) { return; } new Thread() { public void run() { try { Thread.sleep(1 * 1000); } catch (InterruptedException e) { e.printStackTrace(); } int count = 0; boolean isRandom = false; while (count <= 10 && !create_flag) { count++; try { if (mHostInfo != null) { mUserNickName = getNickName(isRandom); muc = ConnectionUtils.getMultiUserChat(mHostInfo.room_id, mHostInfo.room_service, mUserNickName, Password, ChatroomActivity.this); if (muc != null) { create_flag = true; // 创建聊天室成功,监听聊天室返回的消息 // 监听消息 muc.addMessageListener(packetListener); // muc.addParticipantListener(participantListener); muc.addParticipantStatusListener(statusListener); muc.addUserStatusListener(userStatusListener); mHandler.sendEmptyMessageDelayed(9, 500); } else { create_flag = false; } } else { create_flag = false; } // } else { // // } } catch (NotFoundException e) { e.printStackTrace(); } catch (SameException e) {// 昵称重复 isRandom = true; } catch (BannedException e) {// 禁止加入房间 sendBandHandle(15); return; } catch (XMPPException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } if (!create_flag) { android.os.Message message = mHandler.obtainMessage(); message.what = 3; message.obj = getResources().getString(R.string.create_room_fail); message.sendToTarget(); } }; }.start(); }
主要就是创建 muc 对象
muc = ConnectionUtils.getMultiUserChat(mHostInfo.room_id, mHostInfo.room_service, mUserNickName, Password, ChatroomActivity.this);
</pre><pre code_snippet_id="329565" snippet_file_name="blog_20140506_4_2380820" name="code" class="java">然后 添加监听器
<pre code_snippet_id="329565" snippet_file_name="blog_20140506_5_2108603" name="code" class="java">// 创建聊天室成功,监听聊天室返回的消息 / 监听消息 muc.addMessageListener(packetListener); muc.addParticipantStatusListener(statusListener); muc.addUserStatusListener(userStatusListener);
<pre code_snippet_id="329565" snippet_file_name="blog_20140506_6_6329507" name="code" class="java">packetListener :这个是消息监听器。
<pre code_snippet_id="329565" snippet_file_name="blog_20140506_7_2850924" name="code" class="java">statusListener <span style="font-family: Arial, Helvetica, sans-serif;">:</span><span style="font-family: Arial, Helvetica, sans-serif;">聊天室用户状态监听</span>
<pre code_snippet_id="329565" snippet_file_name="blog_20140506_8_7038809" name="code" class="java">userStatusListener<span style="font-family: Arial, Helvetica, sans-serif;">: </span><span style="font-family: Arial, Helvetica, sans-serif;">自身状态监听</span>