1 /**2 * Show notifications in status bar3 */4 Private voidshownotification () {5 //to create a Notificationmanager reference6Notificationmanager Notificationmanager =(Notificationmanager)7 This. Getsystemservice (Android.content.Context.NOTIFICATION_SERVICE); 8 9 //defining various properties of the notificationTenNotification Notification =NewNotification (R.drawable.icon, One"Supervisory System", System.currenttimemillis ()); A //FLAG_AUTO_CANCEL This notification can be cleared by the purge button of the status bar - //flag_no_clear The notification cannot be cleared by the Purge button in the status bar - //flag_ongoing_event notifications are placed in the running the //whether the flag_insistent is always on, such as music always playing, know user response -Notification.flags |= notification.flag_ongoing_event;//place this notification in the "ongoing" or "running" Group of the notification bar -Notification.flags |= notification.flag_no_clear;//indicates that this notification is not cleared and is often used with flag_ongoing_event when you click Clear Notification in the notification bar -Notification.flags |=notification.flag_show_lights; + //Default_all use all default values, such as sound, vibration, splash screen, etc. - //default_lights using default flash hints + //default_sounds using default cue sound A //default_vibrate Use default phone shake, add <uses-permission android:name= "Android.permission.VIBRATE"/> Permissions atNotification.defaults =notification.default_lights; - //Overlay Effect Constants - //notification.defaults=notification.default_lights| Notification.default_sound; -Notification.ledargb =Color.Blue; -NOTIFICATION.LEDONMS = 5000;//flash time, milliseconds - in //set event messages for notifications -Charsequence contenttitle = "steering system title";//Notification Bar Header toCharsequence ContentText = "supervisory system content";//Notification Bar Contents +Intent notificationintent =NewIntent (mainactivity. This, Mainactivity.class);//activity to jump after clicking on the notification -Pendingintent contentitent = pendingintent.getactivity ( This, 0, notificationintent, 0); theNotification.setlatesteventinfo ( This, Contenttitle, ContentText, contentitent); * $ //Pass the notification to Notificationmanager .Panax NotoginsengNotificationmanager.notify (0, notification);
-}
// Delete notification Private void clearnotification () { /// after startup delete the notification we defined before this . Getsystemservice (notification_service); Notificationmanager.cancel (0); }
NotificationManager常用方法介绍:
public
void
cancelAll() 移除所有通知(只是针对当前Context下的Notification)
public
void
cancel(
int
id) 移除标记为id的通知 (只是针对当前Context下的所有Notification)
public
void
notify(String tag ,
int
id, Notification notification) 将通知加入状态栏,标签为tag,标记为id
public
void
notify(
int
id, Notification notification) 将通知加入状态栏,标记为id
Android Notification Notifications