In the previous article, we talked about how to enter this chat room. This time, we will talk about the features in the chat room, chat information, and member changes. Or less nonsense. Theme:
1. chatting is simply a text message. Of course, we cannot get information from the server from time to time. Make full use of real-time push.
(1) first, add a listener, MUC. addmessagelistener (chatlistener). If muc does not know what it is, read the previous article. Chatlistener is our listener. Looking at the code, the code below is actually a bit cool. I'm just too lazy to change it. It's a little annoying. Here we mainly get packet, which is an XML Stream packaged by XMPP with what you want. If you are interested, you can go to the XMPP Chinese translation group and check it out.
/*** Packetlistener provides a mechanism to listen to data packets through a specified filter ** @ author liaonaibo **/class chatpacketlistener implements packetlistener {private string _ number; private date _ lastdate; private multiuserchat _ muc; private string _ roomname; Public chatpacketlistener (multiuserchat MUC) {_ number = "0"; _ lastdate = new date (0); _ muc = muc; _ roomname = MUC. getroom () ;}@ overridepublic void processpacket (packet) {message Message = (Message) packet; string from = message. getfrom (); If (message. getbody ()! = NULL) {delayinformation INF = (delayinformation) message. getextension ("X", "jabber: X: delay"); Date sentdate; If (INF! = NULL) {sentdate = inf. getstamp () ;}else {sentdate = new date () ;}log. I (TAG, "receive old message: Date =" + sentdate. tolocalestring () + "; message =" + message. getbody (); android. OS. message MSG = new android. OS. message (); MSG. what = receive; bundle BD = new bundle (); BD. putstring ("from", from); BD. putstring ("body", message. getbody (); MSG. setdata (BD); handler. sendmessage (MSG );}}}
2. Below is a member. No member in a chat room is too outrageous. What we mainly do is to escape the member list. In fact, there are several ways to do this. I just get the nickname of a member. Some may ask why they do not obtain the member information. I will tell you this next article.
/*** Get all members of the chat room */private void getallmember () {log. I (TAG, "Get all members in the chat room"); affiliates. clear (); New thread (New runnable () {@ overridepublic void run () {try {iterator <string> it = MUC. getoccupants (); While (it. hasnext () {string name = it. next (); name = Name. substring (name. indexof ("/") + 1); affiliates. add (name); log. I (TAG, "member name;" + name) ;}} catch (exception e) {e. printstacktrace ();} android. OS. message MSG = new android. OS. message (); MSG. what = member; handler. sendmessage (MSG );}}). start ();}
At the end of this article, I will write about some changes to the permissions of the chat room, member changes, and theme changes.