<!--A global independent process will be created below->
<service android:name="." android:label="Message Push" android:process=".message" />
<!--or-->
<!--The following will create an application-private:message independent process-->
<service android:name="." android:label="Message Push" android:process=":message" />
We do not need to establish a global one. This article chooses the second solution to create an independent process privately owned by the current application.
3. Notify users and click to view
public class messageservice extends service {
//Get message thread
private messagethread messagethread = null;
//Click to view
private intent messageintent = null;
private pendingintent messagependingintent = null;
//Notification column message
private int messagenotificationid = 1000;
private notification messagenotification = null;
private notificationmanager messagenotificatiomanager = null;
public ibinder onbind(intent intent) {
return null;
}
@override
public int onstartcommand(intent intent, int flags, int startid) {
//initialization
messagenotification = new notification();
= ;
= "New News";
= notification.default_sound;
messagenotificatiomanager = (notificationmanager)getsystemservice(context.notification_service);
messageintent = new intent(this, );
messagependingintent = (this,0,messageintent,0);
//Open thread
messagethread = new messagethread();
= true;
();
return (intent, flags, startid);
}
/**
* Get messages from the server side
*
*/
class messagethread extends thread{
//The operation status, the next step is very useful
public boolean isrunning = true;
public void run() {
while(isrunning){
try {
//Rest for 10 minutes
(600000);
//Get server message
string servermessage = getservermessage();
if(servermessage!=null&&!"".equals(servermessage)){
//Update notification bar
(,"New News","Obama announced that Binla
Brother Deng is dead!"+servermessage,messagependingintent);
(messagenotificationid, messagenotification);
//Every time the notification is completed, the notification id is incremented to avoid overwriting the message
messagenotificationid++;
}
} catch (interruptedexception e) {
();
}
}
}
}
/**
* Here is the server demo, for example only
* @return Return the message to be pushed by the server, otherwise if it is empty, it will not be pushed.
*/
public string getservermessage(){
return "yes!";
}
}