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 have a basic understandingWe need a background service for an independent process,In androidmanifest

When registering a service in. XML, there is an android: process attribute. There are two types of attributes:. And :.. Indicates that a global independent process is started for this service. If it starts with:, an independent process private to this application is started for this service.

 

 

Encoding implementation:

 

Serverpushservice file:

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 {// gets the message thread private messagethread = NULL; // click to view private intent messageintent = NULL; private pendingintent messagependingintent = NULL; // notification bar message private int Messa Genotificationid = 1000; private notification messagenotification = NULL; private icationicationmanager attributes = NULL; @ overridepublic ibinder onbind (intent) {return NULL;} @ overridepublic int onstartcommand (intent, int flags, int startid) {// initialize messagenotification = new notification (); messagenotification. icon = R. drawable. ic_launcher; // notification image messagenotification. tickertex T = "New message"; // notification title messagenotification. defaults = notification. default_sound; messagenotificationmanager = (icationicationmanager) getsystemservice (this. notification_service); // click to view 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 );} /***** get the message from the server * @ author zhanglei **/class messagethread extends thread {// running state public Boolean isrunning = true; @ overridepublic void run () {While (isrunning) {try {// 10 seconds of rest thread. sleep (10000); If (getservermessage (). equals ("yes") {// sets the message content and title messagenoication ication. setlatesteventinfo (serverpushservice. this, "you have a new message! "," This is a new test message ", messagependingintent); // publish messagenoicationicationmanager. Y (messagenotificationid, messagenotification); // to avoid overwriting the message, use ID auto-incrementing messagenotifid ++ ;}} 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

 
<! -- 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". In this way, the service runs in a separate process.ProgramAfter the process is disabled, the offline push is implemented. The annotations in the Code are clear, scanning the server, and determining the logic to publish notifications. The annotations are clearly not described here.

 

 

How to enable the Service:

 
@ Overrideprotected 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.

 

Download source code:

Http://pan.baidu.com/share/link? Consumer id = 457896 & UK = 1997312776

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.