We start with the ANDROIDPN client program entry demoappactivity. The core of the demoappactivity is the following two sentences.
[HTML] View Plaincopyprint? ServiceManager ServiceManager =new ServiceManager (this);
Servicemanager.startservice ();
ServiceManager ServiceManager =new ServiceManager (this);
Servicemanager.startservice ();
Open the ServiceManager class source file, you can see that the constructor completed the server address, port, and other configuration information to write to preferences work. The code for the StartService method is as follows:
[HTML] View plaincopyprint?public void StartService () {
Thread servicethread = new Thread (new Runnable () { @Override public void Run () { Intent Intent = Notifications Ervice.getintent (); Context.startservice (intent); } }); Servicethread.start ();
public void StartService () {
Thread servicethread = new Thread (new Runnable () { @Override public void Run () { Intent Intent = Notifications Ervice.getintent (); Context.startservice (intent); } }); Servicethread.start ();
}
As you can see, it launches the Notificationservice service. Serving this thing, multiple start does not produce multiple instances, and it does not stop unless there is a process to stop it. So the first time the application starts, the service runs there, and even if you quit the application, the client can receive messages from the server side.
Open the Notificationservice class and continue looking at the code.
[HTML] View Plaincopyprint?deviceid =telephonymanager.getdeviceid ();
DeviceId =telephonymanager.getdeviceid ();
It gets the ID of the device, but it doesn't seem to have really used it.
And then
[HTML] View Plaincopyprint?xmppmanager = new Xmppmanager (this);
Tasksubmitter.submit (New Runnable () {public void run () { NotificationService.this.start (); } );
Xmppmanager = new Xmppmanager (this);
Tasksubmitter.submit (New Runnable () {public void run () { NotificationService.this.start (); } );
Now, we're going to connect to the server immediately. Following the NotificationService.this.start method, you can see that it executes the Connect (connect), register (register), login (login) three actions in turn.
Connect is to establish a long connection to the server side.
Register is the client submitting the user name password registration. We found the Registertask class in the Xmppmanager class. It first by looking at the phone's preferences there is no user name and password field to determine whether the user registration, if not registered, it generates a random username password sent past registration. Therefore, if the program has been run once, you also want to change the user name password login is not possible, unless you change users before the user name password removed preferences two attribute values. In addition, the server side does not support the same user name multiple registration, so if you want to do more than the number of accounts at the same time, or change ANDROIDPN server side, or you have to move a bit of brains.
Finally, when the login is complete, the Notificationreceiver class is in the battle when the server sends a message. If you want to do something after you receive the message, modify its OnReceive method. By default, it calls the Notify method of the Notifier class to give a notification.
ANDROIDPN Push Deep