Android XMPP-based instant Messaging

Source: Internet
Author: User

In my spare time, I wrote a simple Instant messenger, based on the OpenFire server platform.

The whole project includes two parts, one is server side, one is Android mobile phone;

One, about the server side nothing to say, download the installation configuration can

It is recommended to download the program with the jar installation, configure the JAVA_JDK can use the compressed package decompression installation

openfire:http://www.igniterealtime.org/downloads/index.jsp

By the way, download a spark, computer-side communication that is used to send messages to and from your Android phone.

Run OpenFire

OpenFire's background Management interface:

Second, focus on the Android side of the program

1, first to connect to the OpenFire server, need a login interface, like this

Code to connect to the server:

Thread thread =NewThread (NewRunnable () { Public voidrun () {Try {            //connecting to a serverxmppconnection.getconnection (). Login (Userstr, PASSSTR); //Connection server succeeded, change online statusPresence Presence =Newpresence (Presence.Type.available);            Xmppconnection.getconnection (). Sendpacket (presence); Handler.sendemptymessage (1); } Catch(xmppexception e) {xmppconnection.closeconnection (); Handler.sendemptymessage (2); }}); Thread.Start ();

Core code for the Xmppconnection class

 Public Static intServer_port = 5222;//Port Public StaticString server_host = "192.168.1.169";//Server Address Public StaticString server_name = "Pearlemon";//Server namePrivate Staticxmppconnection Connection;Private Static voidOpenConnection () {Try {        if(NULL= = Connection | | !connection.isauthenticated ()) {xmppconnection.debug_enabled=true; Connectionconfiguration Conconfig=Newconnectionconfiguration (Server_host, Server_port, server_name); Conconfig.setreconnectionallowed (true); Conconfig.setsendpresence (true); Conconfig.setsaslauthenticationenabled (true); Connection=Newxmppconnection (conconfig);            Connection.connect ();        Configureconnection (); }    } Catch(Xmppexception e) {}} Public Staticxmppconnection getconnection () {if(Connection = =NULL) {openconnection (); }    returnConnection;} Public Static voidCloseConnection () {connection.disconnect (); Connection=NULL;}

2. After connecting to the server, get our friends, like this

Get the code for your buddy list:

Thread thread =NewThread (NewRunnable () { Public voidrun () {Try{Xmppconnection conn=xmppconnection.getconnection (); Roster Roster=Conn.getroster (); Friendlist=NewArraylist<map<string,string>>(); Collection<RosterEntry> entries =roster.getentries (); HashMap<string, string> map =NULL;  for(Rosterentry entry:entries) {map=NewHashmap<string, string>(); Map.put ("Name", Entry.getname ());            Friendlist.add (map); } handler.sendemptymessage (0); } Catch(Exception e) {e.printstacktrace (); }}); Thread.Start ();

3, the Chat interface program, like this

Spark Chat interface

Code to accept the message:

Private Chatmanager Chatman; Private Chat Newchat;
//Message MonitoringChatman =xmppconnection.getconnection (). Getchatmanager (); Newchat= Chatman.createchat (Touserid,NULL); Chatman.addchatlistener (NewChatmanagerlistener () {@Override Public voidchatcreated (chat chat,Booleanable) {Chat.addmessagelistener (NewMessageListener () {@Override Public voidProcessMessage (chat chat, message message) {//receive a message from a PC server (get information from your friends)                if(Message.getfrom (). Contains (Touserid)) {if(Message.getbody (). Length () > 0) {                        //Gets the user, message, time, instring[] args =Newstring[] {touserid, message.getbody ()}; //out of the handler to show the message .Android.os.Message msg =handler. Obtainmessage (); Msg.what= 1; Msg.obj=args;                    Msg.sendtotarget ();    }                }            }        }); }});

Code to send the message:

//Send MessageString Fromuserid =Mappglobal.getname (); String Datestr= Datetimeutils.formatdate (NewDate ()); Chatlist.add (NewMSG (Datestr, Fromuserid, Content, "out"));//Refresh Adaptermadapter.notifydatasetchanged (); mlistview.setselection (Listview.focus_down);//Flush to bottomThread thread =NewThread (NewRunnable () {@Override Public voidrun () {Try{newchat= Chatman.createchat (Touserid + "@8nqa3d40s88hspl",NULL); Message msg=NewMessage ();            Msg.setbody (content); //Send Messagenewchat.sendmessage (msg); } Catch(xmppexception e) {e.printstacktrace (); }}); Thread.Start (); Etsend.settext ("");

Basically is these things, the source to send: Http://files.cnblogs.com/files/pear-lemon/XmppTest.zip

Android XMPP-based instant messaging

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.