Although Google provides GCM for Android Developers to implement push, it needs to install Google Play, Google account, system> 2.2, and Google push server abroad. In China, in Android, you still need to work hard to implement push.
Currently, the most popular open-source push is the XMPP-based androidpn. Androidpn is based on the Mina framework, so here we start with the Mina framework. See the description on the Mina Website:
Server
The following is a simple example to learn how to use Mina. First, create the server. The project uses three jar packages.
Check the Code:
Public class hellomina {Private Static final int Port = 9125;/*** @ Param ARGs * @ throws ioexception */public static void main (string [] ARGs) throws ioexception {// create a non-blocking server socket, using NiO ioacceptor acceptor = new niosocketacceptor (); acceptor. getfilterchain (). addlast ("logger", new loggingfilter (); acceptor. getfilterchain (). addlast ("codec", new protocolcodecfilter (New textlinecodecfactory (charset. forname ("UTF-8"); // sets the message processor acceptor on the server. sethandler (New minaserverhandler (); acceptor. getsessionconfig (). setreadbuffersize (2048); acceptor. getsessionconfig (). setidletime (idlestatus. both_idle, 10); // port bound to the server to start the service acceptor. BIND (New inetsocketaddress (port ));}}
Hellomina processor:
/*** Hellomina processing logic * @ author zhangxy */class minaserverhandler extends iohandleradapter {@ override public void exceptioncaught (iosession session, throwable cause) throws exception {cause. printstacktrace (); Session. close () ;}@ override public void messagereceived (iosession session, object message) throws exception {string STR = message. tostring (); If (Str. trim (). equalsignorecase ("quit") {session. close (); return;} system. err. println ("received message from the client:" + Str); stringbuilder Buf = new stringbuilder (Str. length (); For (INT I = Str. length ()-1; I> = 0; I --) {Buf. append (Str. charat (I);} // and write it back. session. write (BUF. tostring () ;}@ override public void sessionidle (iosession session, idlestatus status) throws exception {system. out. println ("idle" + session. getidlecount (Status);} @ overridepublic void messagesent (iosession session, object message) throws exception {// todo auto-generated method stubsuper. messagesent (Session, message) ;}@ overridepublic void sessionclosed (iosession session) throws exception {// todo auto-generated method stubsuper. sessionclosed (session); system. out. println ("session closed") ;}@ overridepublic void sessioncreated (iosession session) throws exception {// todo auto-generated method stubsuper. sessioncreated (session); system. out. println ("session create") ;}@ overridepublic void sessionopened (iosession session) throws exception {// todo auto-generated method stubsuper. sessionopened (session); system. out. println ("session opened ");}}
Client
The following is the client code. The client does not use NiO and is implemented using a common socket:
Public class hellominaclient {private Socket socket; private dataoutputstream out; private datainputstream in; Public hellominaclient () throws ioexception {}/*** @ Param ARGs */public static void main (string [] ARGs) throws exception {// todo auto-generated method stub hellominaclient minaclient = new hellominaclient (); minaclient. minaclient ();}/*** send message * @ Param out */Public void sendmessa Ge (socket s) {try {out = new dataoutputstream (S. getoutputstream (); out. writebytes ("Mina \ n");} catch (ioexception e) {// todo auto-generated Catch Block E. printstacktrace () ;}} public void receivemessage (socket s) {try {In = new datainputstream (S. getinputstream (); system. err. println ("received message from the server:" + in. readline ();} catch (exception e) {e. printstacktrace () ;}} public void minaclient () THR Ows exception {While (true) {try {socket = new socket ("192.168.21.121", 9124); sendmessage (socket); receivemessage (socket); out. close (); In. close (); thread. sleep (5000);} catch (interruptedexception e) {// todo auto-generated Catch Block E. printstacktrace ();} catch (exception e) {e. printstacktrace ();} finally {try {If (socket! = NULL) socket. Close (); // disconnect} catch (ioexception e) {e. printstacktrace ();}}}}}
The socket is used as a short connection, that is, after receiving and sending messages, the socket is disconnected once, And the thread establishes a connection to receive and send messages after 5 seconds.
/**
* @ Author Zhang xingye
* Email:Xy-Zhang # 163.com
* Android development advanced group:278401545
* Http://blog.csdn.net/xyz_lmn
*/