XMPP Analysis of androidpn

Source: Internet
Author: User
> Transfer from others and forget

========================================================== ======================================
Actively send information to a user
------------------------------------
Xmppconnection. debug_enabled = true;
// Set the server address
Xmppconnection conn = new xmppconnection ("127.0.0.1 ");
Conn. Connect ();
// Enter the account and password to log on
Conn. login ("test2@127.0.0.1", "123456 ");
// Create a dialog with test1 and set the information listener
Chat mychat = conn. getmanager (). Create ("test1@127.0.0.1 ",
New messagelistener (){
@ Override
Public void processmessage (chat, message ){
String messagebody = message. getbody ();
System. Out. println ("received message:" + messagebody );
}
});
// Send the text to test1
Mychat. sendmessage ("hello ");
// Log out
Conn. Disconnect ();
========================================================== ======================================
Set your login status
------------------------------------
Xmppconnection. debug_enabled = true;
// Set the server address
Xmppconnection conn = new xmppconnection ("127.0.0.1 ");
Conn. Connect ();
// Enter the account and password to log on
Conn. login ("test2@127.0.0.1", "123456 ");

// Set the personal status information after login
Presence P = new presence (presence. type. available );
P. setstatus ("in a daze... ");
Conn. sendpacket (P );

// Log out
Conn. Disconnect ();
========================================================== ========================================
Passively receive user messages
------------------------------------
Xmppconnection. debug_enabled = true;
// Set the server address
Xmppconnection conn = new xmppconnection ("127.0.0.1 ");
Conn. Connect ();
// Enter the account and password to log on
Conn. login ("test1@127.0.0.1", "123456 ");
// Set the listener Information
Conn. getchatmanager (). addchatlistener (New chatmanagerlistener (){
@ Override
Public void chatcreated (chat, Boolean createdlocally ){
Chat. addmessagelistener (New messagelistener (){
@ Override
Public void processmessage (chat, message ){
String messagebody = message. getbody ();
System. Out. println ("received information:" + messagebody );
}
});
}
});
// Log out
Conn. Disconnect ();
========================================================== ========================================
Get my friend list
------------------------------------
// Set the server address
Xmppconnection conn = new xmppconnection ("127.0.0.1 ");
Conn. Connect ();
// Enter the account and password to log on
Conn. login ("test1@127.0.0.1", "123456 ");
Collection Rosters = conn. getroster (). getentries ();
System. Out. println ("My friend list: ======================= ");
For (rosterentry: rosters ){
System. Out. Print ("name:" + rosterentry. getname () + ", jid:" + rosterentry. getuser ());
System. Out. println ("");
}
System. Out. println ("My friend list: ======================= ");

Conn. Disconnect ();
========================================================== ========================================
Chat Window input status, using XEP-0085 Protocol
------------------------------------
// Send the initial text to test1 with the input status
Message mess = new message ();
Mess. addextension (New chatstateextension (chatstate. Active ));
Mychat. sendmessage (MESS );
// Send the initial text information to test1 with the input status
Message mess = new message ();
Mess. addextension (New chatstateextension (chatstate. composing ));
Mychat. sendmessage (MESS );

// Send the initial text message to test1, with the paused input status
Message mess = new message ();
Mess. addextension (New chatstateextension (chatstate. paused ));
Mychat. sendmessage (MESS );
// Others...

// When receiving
Public void processmessage (chat, message ){
String messagebody = message. getbody ();
Packetextension PE;

Pe = message. getextension ("composing", "http://jabber.org/protocol/chatstates ");
If (PE! = NULL ){
System. Out. println ("the other party is inputting ......");
}

Pe = message. getextension ("active", "http://jabber.org/protocol/chatstates ");
If (PE! = NULL ){
System. Out. println ("received information:" + messagebody );
}

Pe = message. getextension ("paused", "http://jabber.org/protocol/chatstates ");
If (PE! = NULL ){
System. Out. println ("the other party has paused the input ");
}

Pe = message. getextension ("inactive", "http://jabber.org/protocol/chatstates ");
If (PE! = NULL ){
System. Out. println ("the recipient's chat window loses focus ");
}

Pe = message. getextension ("gone", "http://jabber.org/protocol/chatstates ");
If (PE! = NULL ){
System. Out. println ("the peer chat window is closed ");
}
}
========================================================== ========================================
Receive invitations and join multi-person chat rooms
------------------------------------
Multiuserchat. addinvitationlistener (Conn, new invitationlistener (){
@ Override
Public void invitationreceived (xmppconnection Conn, string room,
String inviter, string reason, string password, message ){
Multiuserchat = new multiuserchat (Conn, room );
System. Out. println ("received from" + inviter +. Content included with the invitation: "+ reason );
Try {
Multiuserchat. Join ("Test2", password );
} Catch (xmppexception e ){
System. Out. println ("failed to join the chat room ");
E. printstacktrace ();
}
System. Out. println ("successfully added to the chat room ");
Multiuserchat. addmessagelistener (New packetlistener (){
@ Override
Public void processpacket (packet ){
Message message = (Message) packet;
// Receives chat information from the chat room
System. Out. println (message. getfrom () + ":" + message. getbody ());
}
});
}
// Send the message to the chat room
Multiuserchat. sendmessage ("coming soon! ");
});

========================================================== ========================================
Log on to Gtalk
------------------------------------
Xmppconnection conn = new xmppconnection (New connectionconfiguration ("talk.google.com", 5222, "gmail.com "));
Conn. Connect ();
// Enter the password of the Gtalk account
Conn. login ("88888888", "8888888888 ");
Collection Rosters = conn. getroster (). getentries ();
// Obtain the friend list on Gtalk
System. Out. println ("My friend list: ======================= ");
For (rosterentry: rosters ){
System. Out. Print ("name:" + rosterentry. getname () + ", jid:" + rosterentry. getuser ());
System. Out. println ("");
}
System. Out. println ("My friend list: ======================= ");

Conn. Disconnect ();
========================================================== ========================================

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.