First, the principle
There are two kinds of message push, one is that the client can search the message directly to the server, get the message if new message is found, and the server sends a message to the client, that is, when there is an informational message, the server sends a message to the client.
Second, Step (code)
Note: Notification//is a specific status bar object, set icon, text, sound, and so on.
Notificationmangager//status bar notifies the management class, is responsible for sending messages, cleaning up messages.
Import android.app.Notification;
Import Android.app.NotificationManager;
Import android.app.PendingIntent;
Import Android.app.Service;
Import Android.content.Context;
Import android.content.Intent;
Import Android.net.Uri;
Import Android.os.IBinder;
Import Android.provider.MediaStore.Audio;
Import Android.util.Log;
Import Android.widget.RemoteViews;
/**
* Message push
*
* @author Msquirrel
*
*/
public class Messageservice extends Service {
Private String TAG = "-----------";
Private Messagethread messagethread = null;
Click to view
Private Intent messageintent = null;
Private pendingintent messagependingintent = null;
Notification bar messages
private int messagenotificationid = 1000;
Private Notification messagenotification = null; is the specific status bar notification object, you can set the icon, text, cue sound, vibration and so on parameters.
Private Notificationmanager Messagenotificatiomanager = null; is the status bar notification management class, responsible for sending notifications, clear notification and so on.
Private remoteviews Contentview = null;
@Override
Public IBinder Onbind (Intent Intent) {
TODO auto-generated Method Stub
return null;
}
@Override
public int Onstartcommand (Intent Intent, int flags, int startid) {
Initialization
Messagenotification = new
Notification (R.drawable.icon, "new Message One", System.currenttimemillis ()); * This version is not used in the//* simple message version
Messagenotification = new Notification ();
Messagenotification.icon = r.drawable.icon;//status bar hint icon
Messagenotification.tickertext = "Hey, test message push";//status bar prompt message
Contentview = new Remoteviews (Getpackagename (), r.layout.view);//message content Container
Contentview.setimageviewresource (R.id.image, R.drawable.icon);//icon for Message container interface
Messagenotification.contentview = contentview;//binds the message container and the message
Messagenotification.icon = r.drawable.icon;//* in the simple message version * This version does not use
Messagenotification.tickertext = "new Message 11"; * This version is not used in the//* simple message version
Messagenotification.when=system.currenttimemillis ();
* This version is not used in the simple message version
Messagenotification.defaults |= notification.default_sound;//Sound
Messagenotification.defaults |= notification.default_lights;//Lamp
Messagenotification.defaults |= notification.default_vibrate;//Shock
Messagenotification.sound = Uri.parse ("File:///sdcard/to.mp3");
Messagenotification.sound = Uri.withappendedpath (
Audio.Media.INTERNAL_CONTENT_URI, "2");//Select the 2nd song in the music list to make the message sound
Messagenotification.ledargb = Color of 0xff00ff00;//lamp
MESSAGENOTIFICATION.LEDONMS = 300; The time of the light
MESSAGENOTIFICATION.LEDOFFMS = 1000; Time to Kill
Messagenotification.flags |= notification.flag_show_lights;//Display Light
Long v[]= {0,100,200,300}; Vibration Frequency
Messagenotification.vibrate = v;
//
Messagenotification.flags |= notification.flag_auto_cancel;//After clicking the message, the message exits automatically
Messagenotification.flags |= notification.flag_ongoing_event;//appears in the top run message Bar
messagenotification.flags|=notification.flag_no_clear;//This message will not be cleared
Messagenotificatiomanager = (Notificationmanager) getsystemservice (Context.notification_service);
Messageintent = new Intent (this, showmessage. Class);//Click on the message, the interface to jump (corresponding to the interface of the detailed message)
Open Thread
Messagethread = new Messagethread ();//The thread publishes a message every 10 seconds.
Messagethread.isrunning = true;//is set to false, the thread jumps out of the loop and ends the
Messagethread.start ();
LOG.I (TAG, "Startcommand");
Return Super.onstartcommand (Intent, flags, Startid);
}
/**
* Get messages from server side
*/
Class Messagethread extends Thread {
When set to False, the thread jumps out of the loop and ends
public Boolean isrunning = true;
public void Run () {
while (isrunning) {
try {
String servermessage = Getservermessage ();
if (servermessage! = NULL &&! "". Equals (ServerMessage)) {
Update Notification Bar
Messagenotification.setlatesteventinfo (Messageservice.this, "New Message", "Wow ~ There's new news yes!") +servermessage,messagependingintent);//* in the simple message version * This version does not use
Contentview.settextviewtext (R.id.text, servermessage);//Set Message contents
Messageintent.putextra ("message", servermessage);//Add Parameters for intent
Messagependingintent = Pendingintent.getactivity (
Messageservice.this, 0, Messageintent,
pendingintent.flag_cancel_current);//The intention is to load the deferred intent
Messagenotification.contentintent = messagependingintent;//will delay intent loading messages
Messagenotificatiomanager.notify (Messagenotificationid,
messagenotification);//Start notification
LOG.I (TAG, "send Message");
Messagenotificatiomanager.cancel (messageNotificationID-1);//After a new message is sent, remove the previous message (only the latest message is displayed)
Configure the ID number of the next message
messagenotificationid++;
}
Rest for 10 seconds.
Thread.Sleep (10000);
Get Server Messages
} catch (Interruptedexception e) {
E.printstacktrace ();
}
}
}
}
/**
* Imitate the message sent by the server, only as an example.
*
* @return Returns the message that the server wants to push, or if it is empty, do not push
*/
Public String Getservermessage () {
LOG.I (TAG, "getmessage");
Return "Pro, Test success ~~!";
}
@Override
public void OnDestroy () {
System.exit (0);
Messagethread.isrunning = false;
Alternatively, choose one, System.exit (0) is recommended so that the process exits cleaner
Messagethread.isrunning = false;
Super.ondestroy ();
LOG.I (TAG, "destroy");
}
}
address of this article:Http://www.bhcode.net/article/20140302/22603.html