Android chat room developed based on XMPP smack openfire (2) [CHAT information and members]

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.