Android uses a separate Service process to simulate offline Server Push

Source: Internet
Author: User

Overview:

First of all, I will briefly describe my understanding of message pushing. Here I will take QQ as an example. When QQ is offline on our mobile phone and we quit the QQ application, however, if someone sends us a message at this time, we are not online. The server will push the information sent by the sender, and then we will release a notification to display the notification to our users.

 

 

Principles:

Based on the above overview, we basically understand that we need an independent process Background Service, in AndroidManifest

. When registering a Service in xml, there is an android: process attribute, which has two conditions. and. indicates that a global independent process is enabled for this service. If it starts with:, an independent process private to this application is enabled for this service.

 

 

Encoding implementation:


ServerPushService file:

[Java]
Import android. app. Notification;
Import android. app. icationicationmanager;
Import android. app. PendingIntent;
Import android. app. Service;
Import android. content. Intent;
Import android. OS. IBinder;
 
Public class ServerPushService extends Service {
// Obtain the message thread
Private MessageThread messageThread = null;
// Click to View Details
Private Intent messageIntent = null;
Private PendingIntent messagePendingIntent = null;
// Notification bar message
Private int messageNotificationID = 1000;
Private Notification messageNotification = null;
Private icationicationmanager messageNotificationManager = null;
@ Override
Public IBinder onBind (Intent intent ){
Return null;
}
@ Override
Public int onStartCommand (Intent intent, int flags, int startId ){
// Initialization
MessageNotification = new Notification ();
MessageNotification. icon = R. drawable. ic_launcher; // notification Image
MessageNotification. tickerText = "New message"; // notification title
MessageNotification. defaults = Notification. DEFAULT_SOUND;
MessageNotificationManager = (icationicationmanager) getSystemService (this. icationication_service );
// Click to View Details
MessageIntent = new Intent (this, MessageActivity. class );
MessagePendingIntent = PendingIntent. getActivity (this, 0, messageIntent, 0 );
// Enable the thread
MessageThread thread = new MessageThread ();
Thread. isRunning = true;
Thread. start ();
Return super. onStartCommand (intent, flags, startId );
}
 
/***
* Retrieve messages from the server
* @ Author zhanglei
*
*/
Class MessageThread extends Thread {
// Running status
Public boolean isRunning = true;
@ Override
Public void run (){
While (isRunning ){
Try {
// Rest for 10 seconds
Thread. sleep (10000 );
If (getServerMessage (). equals ("yes ")){
// Set the message content and title
MessageNotification. setLatestEventInfo (ServerPushService. this, "you have a new message! "," This is a new test message ", messagePendingIntent );
// Publish a message
MessageNotificationManager. Y (messageNotificationID, messageNotification );
// Avoid overwriting messages and use ID auto-Increment
MessageNotificationID ++;
}
} Catch (Exception e ){
E. printStackTrace ();
}
}
}
}
/***
* Simulates messages on the server. In actual application, you should go to the server to get the message
* @ Return
*/
Public String getServerMessage (){
Return "yes ";
}
}

Import android. app. Notification;
Import android. app. icationicationmanager;
Import android. app. PendingIntent;
Import android. app. Service;
Import android. content. Intent;
Import android. OS. IBinder;

Public class ServerPushService extends Service {
// Obtain the message thread
Private MessageThread messageThread = null;
// Click to View Details
Private Intent messageIntent = null;
Private PendingIntent messagePendingIntent = null;
// Notification bar message
Private int messageNotificationID = 1000;
Private Notification messageNotification = null;
Private icationicationmanager messageNotificationManager = null;
@ Override
Public IBinder onBind (Intent intent ){
Return null;
}
@ Override
Public int onStartCommand (Intent intent, int flags, int startId ){
// Initialization
MessageNotification = new Notification ();
MessageNotification. icon = R. drawable. ic_launcher; // notification Image
MessageNotification. tickerText = "New message"; // notification title
MessageNotification. defaults = Notification. DEFAULT_SOUND;
MessageNotificationManager = (icationicationmanager) getSystemService (this. icationication_service );
// Click to View Details
MessageIntent = new Intent (this, MessageActivity. class );
MessagePendingIntent = PendingIntent. getActivity (this, 0, messageIntent, 0 );
// Enable the thread
MessageThread thread = new MessageThread ();
Thread. isRunning = true;
Thread. start ();
Return super. onStartCommand (intent, flags, startId );
}

/***
* Retrieve messages from the server
* @ Author zhanglei
*
*/
Class MessageThread extends Thread {
// Running status
Public boolean isRunning = true;
@ Override
Public void run (){
While (isRunning ){
Try {
// Rest for 10 seconds
Thread. sleep (10000 );
If (getServerMessage (). equals ("yes ")){
// Set the message content and title
MessageNotification. setLatestEventInfo (ServerPushService. this, "you have a new message! "," This is a new test message ", messagePendingIntent );
// Publish a message
MessageNotificationManager. Y (messageNotificationID, messageNotification );
// Avoid overwriting messages and use ID auto-Increment
MessageNotificationID ++;
}
} Catch (Exception e ){
E. printStackTrace ();
}
}
}
}
/***
* Simulates messages on the server. In actual application, you should go to the server to get the message
* @ Return
*/
Public String getServerMessage (){
Return "yes ";
}
}


Register the service in a separate process

[Html]
<! -- Private independent process for this application -->
<Service
Android: name = "com. jay. serverpush. ServerPushService"
Android: process = ": message"
>
</Service>

<! -- Private independent process for this application -->
<Service
Android: name = "com. jay. serverpush. ServerPushService"
Android: process = ": message"
>
</Service>


Note: a service is written in this file to run in the background. In manifest, the service is declared as starting with "SS" to run in a separate process, to achieve offline push after the program is closed and the process is not closed, the annotations in the Code are clear, scanning the server and determining the logic to publish notifications, etc, the annotations are clearly not described here

 


How to enable the Service:

[Java]
Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
This. startService (new Intent (this, ServerPushService. class ));
}
 
 
This. startService (new Intent (this, ServerPushService. class ));

@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
This. startService (new Intent (this, ServerPushService. class ));
}


This. startService (new Intent (this, ServerPushService. class ));


In this statement, the service of a separate process is enabled when the oncreate method is started for the first time.

 

 

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.