Notification is the notification displayed in the phone status bar
Create a notification object from the Notification.builder class.
Notification.builder Common methods:
SetDefaults (): Set notification LED lights, music, vibration, etc.
Setautocancle (): Set the status bar to automatically delete notifications after clicking notifications
Setcontenttitle (): Set notification title
Setcontenttext (): Set notification content
Setsmallcon (): Set small icon
Setlargecon (): Set Large icon
Settick (): Set notification in status bar for this
Setcontentintent (): Sets the pendingintent of the program component to be launched after the click Notification
Setwhen (): Set the time for notification publication
Steps:
1. Call the Getsystemservice (Notification_service) method to get the Notificationmanager method of the system
Manager = (Notificationmanager) getsystemservice (Context.notification_service);
2. Create a Notification.builder object
Notification.builder Builder = new Notification.builder (mainactivity.this);
3. Setting various properties for builder
4. Create a Notification object
Notification Notification = Builder.build ();
5. Send notification via Notificationmanager's Notify method
Manager.notify (ID, notification);
Demo:
Activity:
public class Mainactivity extends Activity {Button send, Del; Notificationmanager manager;int ID = 0x123; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); send = (Button) Findviewbyid (r.id.send);d el = (Button) Findviewbyid (r.id.del); manager = (Notificationmanager) getsystemservice (Context.notification_service); Send.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {Intent Intent = new Intent ( Mainactivity.this, Other.class); pendingintent pi = pendingintent.getactivity (mainactivity.this,0, intent, 0); Notification.builder Builder = new Notification.builder (mainactivity.this); builder//Notification Notification = new// Notification.builder (Mainactivity.this)//Set open notification, the notification cancels. Setautocancel (TRUE)//Set notification prompt information. Setticker ("new Message")// Sets the icon for the notification. Setsmallicon (R.drawable.pig)//sets the caption of the notification. Setcontenttitle ("Not good!!!" ")//Set the content of the notification. Setcontenttext (" Your home Pig ran ")//Set use the system default sound, Led.setdefaults (notification.default_lights| Notification.default_sound)//Set notification release time. Setwhen (System.currenttimemillis ())//Set the activity to start. Setcontentintent (PI). Build (); Notification Notification = Builder.build (); Manager.notify (ID, Notification);}}); Del.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {manager.cancel (ID);}});}}
Click Send notification:
Clicking the notification jumps to another activity:
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Easy use of Android------Notification notification bar