Android server push for androidpn Analysis

Source: Internet
Author: User
> Most of the currently widely used server push technologies are encapsulated in XMPP protocol.
> Have you ever touched XMPP? In Linux, some IM clients will allow you to add accounts that support the XMPP protocol, such as ICQ and MSN by default. In addition, QQ is also based on XMPP, gmail and Gtalk in Android are also based on XMPP protocol.
> The following is a simple analysis of androidpn encapsulation of server push technology in Android. In the future, we will analyze and learn smack encapsulation of Android XMPP protocol.
> Androidpn is also built on the XMPP protocol. Fortunately, it encapsulates the server and client, which is easy to use and expand, improving the efficiency of many developers, this is also the best reason to choose it.

Simple client process
---
Step 1: configure the client
Go to the project '-> res-> raw-> androidpn. properties' file

Apikey = 1234567890 # key
Xmpphost = 192.168.1.1 # IP
Xmppport = 5222 # Port

Step 2:

// Create a new service
Servicemanager = new servicemanager (this );
// Set the notification bar icon
Servicemanager. seticationicationicon (R. drawable. Notification );
// Start the service
Servicemanager. startservice ();

Detailed analysis
---
Initialize servicemanager:

This. Context = context;

// Obtain the Class Name of the caller Activity Package name.
If (context instanceof activity ){
Log. I (logtag, "Callback activity ...");
Activity callbackactivity = (activity) context;
Callbackactivitypackagename = callbackactivity. getpackagename ();
Callbackactivityclassname = callbackactivity. getclass (). getname ();
}

// Loadproperties () reads the androidpn. properties file in raw and returns the properties object

Props = loadproperties ();
Apikey = props. getproperty ("apikey ","");
Xmpphost = props. getproperty ("xmpphost", "127.0.0.1 ");
Xmppport = props. getproperty ("xmppport", "5222 ");

// Save the properties obtained above to sharedpreferences for future direct calls
Sharedprefs = context. getsharedpreferences (
Constants. shared_preference_name, context. mode_private );
Editor editor = sharedprefs. Edit ();
...
...
...
Editor. Commit ();

Start the service startservice ()

// Start the service with a thread
Thread servicethread = new thread (New runnable (){
@ Override
Public void run (){
Intent intent = icationicationservice. getintent ();
Context. startservice (intent );
}
});
Servicethread. Start ();

Notificationservice class analysis, which is a subclass of a service, focuses on analyzing this service

Public notificationservice (){

/* Icationicationreceiver is a subclass of broadcastreceiver.
* Used to receive push broadcasts and notify users with icationicationmanager
* That is, the notification in the system notification bar
*/
Notificationreceiver = new notificationrece

/* Connectivityreceiver receives the network status broadcast of the mobile phone
* Manages the connection and disconnection between xmppmanager and the server
*/
Connectivityreceiver = new connectivityreceiver (this );

/* Integrated with Android. telephony. phonestatelistener,
* Same as above, used to monitor the status of data links
*/
Phonestatelistener = new phonestatechangelistener (this );

// Thread Pool
Executorservice = executors. newsinglethreadexecutor ();

/* The tasksubmitter class contains a task submitted to the above thread pool.
* Method
*/
Tasksubmitter = new tasksubmitter (this );

/* Task counters
* The task used to maintain the current job
*/
Tasktracker = new tasktracker (this );
}

After all the statements are made, the service will be executed.

Private void start (){
Log. D (logtag, "Start ()...");

// Register the notification Receiver
Registericationicationreceiver ();

// Register the receiver of the mobile phone network connection status
Registerconnectivityreceiver ();
// Intent = getintent ();
// Startservice (intent );

// Start the XMPP persistent connection with the server
// There will be an analysis after xmppmanager
Xmppmanager. Connect ();
}

Xmppmanager manages XMPP links:

Public xmppmanager (icationicationservice notificationservice ){
Context = icationicationservice;

// Obtain the task submission manager, which is used to maintain parallel tasks.
Tasksubmitter = icationicationservice. gettasksubmitter ();

// Task counter
Tasktracker = icationicationservice. gettasktracker ();

// The configuration information is obtained below
Sharedprefs = notificationservice. getsharedpreferences ();
Xmpphost = sharedprefs. getstring (constants. xmpp_host, "localhost ");
Xmppport = sharedprefs. getint (constants. xmpp_port, 5222 );
Username = sharedprefs. getstring (constants. xmpp_username ,"");
Password = sharedprefs. getstring (constants. xmpp_password ,"");

/* Set the listener of the XMPP link status. Check the code and find that there are 5 XMPP link statuses.
* 1 connectionclosed
* 2 connectionclosedonerror
* 3 reconnectingin
* 4 reconnectionfailed
* 5 reconnectionsuccessful
*/
Connectionlistener = new persistentconnectionlistener (this );

/* Server push listener
* If a message is pushed to the server, icationicationpacketlistener will
* Resolve it by yourself and send broadcasts through xmppmanager
*/
Icationicationpacketlistener = new notificationpacketlistener (this );

// When XMPP reconnects to the server due to an exception and an exception occurs during this period, it will be handled in this handler
Handler = new handler ();

// Task queue
Tasklist = new arraylist ();

/* When xmppmanager is disconnected from the server due to an exception
* Reconnectionthread will try to reconnect within a certain period of time
* That is, when the persistentconnectionlistener listener listens to an exception and disconnects
* The reconnection method in reconnectionthread is called to try the connection.
Reconnection = new reconnectionthread (this );
}

How to connect androidpn to the server
---
This involves many operations on the smack package. The next article will analyze the encapsulated smack of the XMPP protocol in Android.

_ Runable 1: connecttask _
Establish a connection with the server

_ Runable 1.5: registertask _
If the account information of the androidpn client is not configured, it automatically generates a random account and registers it to the server.

_ Runalbe 2: logintask _
Read the local account information, log on, and start waiting for the server to push messages.

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.