ANDROID Notification Notification details

Source: Internet
Author: User

Depending on the activity's life cycle, when the activity does not show, the OnStop function (such as pressing the Home key) is executed, so you put notification in the notification bar in the OnStop function (except by pressing the Exit key), Remove the notification from the notice bar. Or, the notification bar icon is displayed as long as the program is running.

The following is a brief introduction to some constants, fields, and methods in the notification class: constants: Default_all use all default values, such as sound, vibration, splash screen, etc. default_lights use the default flash hint Default_sounds Use default prompt sound default_vibrate Use default phone Vibrate "description": Add phone Shake, be sure to add permissions in Manifest.xml: <uses-permission android:name= " Android.permission.VIBRATE "/> Above the effect constants can be superimposed, that is, by Notification.defaults =default_sound|   Default_vibrate; Notification.defaults |= Default_sound (preferably tested on a real machine, not on the vibration effect simulator)

Set FLAG bit FLAG_AUTO_CANCEL the notification can be cleared by the status bar's Purge button flag_no_clear the notification can be cleared by the status bar's Purge button flag_ongoing_event notification is placed in the running Flag_insist Is ENT always on, like music always playing, knowing user response

Characters commonly used segment: Contentintent set Pendingintent object, click to send the intent defaults add default effect flags set flag bit, such as flag_no_clear icon settings icons Sound Ti Ckertext displays the text in the status bar when the timestamp for this notification is sent

Notificationmanager Common method Description: public void Cancelall () removes all notifications (only for notification under current context) public void cancel (int id) Remove the notification marked as ID (just for all Notification under the current context) public void notify (String tag, int id, Notification Notification) to add the notification to the status bar, label For tag, mark as id public void notify (int id, Notification Notification) to add notification to the status bar, mark as ID

[Java]View Plaincopyprint?
  1. Package com.ljq.activity;
  2. Import android.app.Activity;
  3. Import android.app.Notification;
  4. Import Android.app.NotificationManager;
  5. Import android.app.PendingIntent;
  6. Import android.content.Intent;
  7. Import Android.graphics.Color;
  8. Import Android.os.Bundle;
  9. Public class Mainactivity extends Activity {
  10. /** Called when the activity is first created. * /
  11. @Override
  12. public void OnCreate (Bundle savedinstancestate) {
  13. super.oncreate (savedinstancestate);
  14. Setcontentview (R.layout.main);
  15. Clearnotification ();
  16. }
  17. @Override
  18. protected void OnStop () {
  19. Shownotification ();
  20. Super.onstop ();
  21. }
  22. @Override
  23. protected void OnStart () {
  24. Clearnotification ();
  25. Super.onstart ();
  26. }
  27. /** 
  28. * Show notifications in status bar
  29. */
  30. private void Shownotification () {
  31. //Create a Notificationmanager reference
  32. Notificationmanager Notificationmanager = (Notificationmanager)
  33. This.getsystemservice (Android.content.Context.NOTIFICATION_SERVICE);
  34. //define various properties of notification
  35. Notification Notification =new Notification (R.drawable.icon,
  36. "Supervisory System", System.currenttimemillis ());
  37. //FLAG_AUTO_CANCEL This notification can be cleared by the purge button of the status bar
  38. //flag_no_clear The notification cannot be cleared by the Purge button in the status bar
  39. //flag_ongoing_event notifications are placed in the running
  40. whether the//flag_insistent is always on, such as music always playing, know user response
  41. Notification.flags |= notification.flag_ongoing_event; //place this notification in the "ongoing" or "running" Group of the notification bar
  42. Notification.flags |= notification.flag_no_clear; //Indicates that this notification is not cleared after clicking "Clear Notification" in the notification bar, and is often used with flag_ongoing_event
  43. Notification.flags |= notification.flag_show_lights;
  44. //default_all use all default values, such as sound, vibration, splash screen, etc.
  45. //default_lights using default flash hints
  46. //default_sounds using default cue sound
  47. //default_vibrate Use default phone shake, add <uses-permission android:name= "Android.permission.VIBRATE"/> Permissions
  48. Notification.defaults = notification.default_lights;
  49. //Overlay Effect Constants
  50. //notification.defaults=notification.default_lights|  Notification.default_sound;
  51. Notification.ledargb = Color.Blue;
  52. NOTIFICATION.LEDONMS =5000; //Flash time, Ms
  53. //Set event messages for notifications
  54. Charsequence contenttitle ="steering system title"; //Notification bar header
  55. Charsequence ContentText ="supervisory system content"; //Notification bar contents
  56. Intent notificationintent =new Intent (mainactivity. This, mainactivity. class); //Click on the activity to jump after the notification
  57. Pendingintent contentitent = pendingintent.getactivity (this, 0, Notificationintent, 0);
  58. Notification.setlatesteventinfo (This, Contenttitle, ContentText, contentitent);
  59. //Pass the notification to Notificationmanager
  60. Notificationmanager.notify (0, notification);
  61. }
  62. //Delete notification
  63. private void Clearnotification () {
  64. //Start Delete before we define the notification
  65. Notificationmanager Notificationmanager = (notificationmanager) This
  66. . Getsystemservice (Notification_service);
  67. Notificationmanager.cancel (0);
  68. }
  69. }

ANDROID Notification Notification details

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.