A notification mechanism appears at the top of the mobile phone when the notification bar is displayed. After the notification is opened, the notification information is displayed. You can also interact with the user and process user selection events.
I. Create
1) obtain the icationicationmaneger object
// Obtain the icationicationmanager object icationicationmanager manager = (NotificationManager) this. getSystemService (Context. icationication_service ); |
2) instantiate the Notification object
// Initialize Notification object Notification notification = new Notification (); |
3) set Notification Object Attributes
// Set the icon notification for the status bar at the top of the mobile phone. icon = R. drawable. ic_launcher; // The notification message displayed when the current notification is placed on the status bar. tickerText = "NOTE "; |
4) provides the response action after the user clicks
// Provides the jump information Intent intent = new Intent (this, Info. class); // triggered when the user clicks PendingIntent pending = PendingIntent. getActivity (this, 0, intent, PendingIntent. FLAG_ONE_SHOT ); |
5) set the prompt information displayed when you click the icon in the status bar. parameters: (Activity object, title information, text information, PendingIntent object)
Notification. setLatestEventInfo (this, "content prompt:", "I am a test file", pending ); |
6) Use icationicationmaneger's notify (Notification ID, Notification object) method to send a Notification. The notification ID is used to prepare for future cancellation notifications)
manager.notify(1, notification); |
II. Specific applications
Function: Provides the notification bar. The action in the pop-up prompt box is triggered after the user clicks it.
◆ Main Activity
@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // obtain the icationicationmanager object icationicationmanager manager = (icationicationmanager) this. getSystemService (Context. NOTIFICATION_SERVICE); // initialize the Notification object Notification = new notification (); // you can specify the icon Notification for the status bar at the top of the mobile phone. icon = R. drawable. ic_launcher; // The notification message displayed when the current notification is placed on the status bar. tickerText = "NOTE"; // Add a sound prompt // notification. defaults = Notification. DEFAULT_SOUND; // The value of audioStreamType must be the value in AudioManager, representing the ringing mode // notification. audioStreamType = // android. media. audioManager. ADJUST_LOWER; // The jump information Intent intent = new Intent (this, Info. class); // triggered when the user clicks PendingIntent pending = PendingIntent. getActivity (this, 0, intent, PendingIntent. FLAG_ONE_SHOT); // click the notification message setting (Activity object, title information, text information, PendingIntent object) displayed on the icon of the status bar. setLatestEventInfo (this, "content prompt:", "I am a test file", pending); // start notification manager. Y (1, notification );} |
◆ Process information Activity
@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R.layout.info); // The prompt box AlertDialog. builder dia = (Builder) new AlertDialog. builder (this); dia. setTitle ("prompt"); dia. setMessage ("I am the prompt in the notification bar"); dia. setPositiveButton ("OK", new OnClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {Toast. makeText (Info. this, "you have clicked OK", Toast. LENGTH_LONG ). show () ;}}); dia. setNegativeButton ("cancel", new OnClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {Toast. makeText (Info. this, "you have clicked the cancel button", Toast. LENGTH_LONG ). show () ;}}); dia. create (); dia. show (); // obtain the icationicationmanager object icationicationmanager manager = (icationicationmanager) this. getSystemService (Context. NOTIFICATION_SERVICE); // cancel the notification to avoid interfering with the User manager. cancel (1 );} |
◆ Result
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1056205410-0.png "title =" .png "/>
It's not easy to do anything .. What is the difference between drinking coffee and drinking water? 650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1056206259-1.gif "/>
This article is from the "Schindler" blog, please be sure to keep this source http://cinderella7.blog.51cto.com/7607653/1282174