1 Public classNotificationtestextendsActivity2 {3 Static Final intnotification_id = 0x123;4 notificationmanager nm;5 6 @Override7 Public voidonCreate (Bundle savedinstancestate)8 {9 Super. OnCreate (savedinstancestate);Ten Setcontentview (r.layout.main); One //get the Notificationmanager service for your system ANM =(Notificationmanager) - Getsystemservice (notification_service); - } the - //defines an event-handling method for the Click event of a button that sends notifications - Public voidSend (View source) - { + //Create a intent that initiates other activity -Intent Intent =NewIntent (notificationtest. This +, Otheractivity.class); APendingintent pi =pendingintent.getactivity ( atNotificationtest. This, 0, intent, 0); -Notification notify =NewNotification.builder ( This) - //set to turn on the notification, the notification disappears automatically -. Setautocancel (true) - //set up a notification prompt that appears in the status bar -. Setticker ("New News") in //to set the icon for a notification - . Setsmallicon (r.drawable.notify) to //set the title of the notification content +. Setcontenttitle ("A new notice") - //Set notification content the. Setcontenttext ("Congratulations, you've got a raise, you're paying 20% more!")) * // //set Use system default sound, default LED light $ //. SetDefaults (Notification.default_soundPanax Notoginseng // | Notification.default_lights) - //set a custom sound for notifications the. Setsound (Uri.parse ("android.resource://org.crazyit.ui/" ++r.raw.msg)) A . Setwhen (System.currenttimemillis ()) the //change notification to start the program intent + . Setcontentintent (PI). build (); - //Send Notifications $ Nm.notify (notification_id, notify); $ } - - //defines the event handling method for the Click event of the button that deletes the notification the Public voiddel (View v) - {Wuyi //Cancel Notification the Nm.cancel (notification_id); - } Wu}
Android Notification notifications