Notifications (NotificationManager and Notification) and androidmanager in Android

Source: Internet
Author: User

Notifications (NotificationManager and Notification) and androidmanager in Android

Next, let's talk about notification. This notification is generally used by phone, SMS, email, and Alarm ringtone. A small icon will appear on the status bar of the mobile phone, prompting the user to process the notification, then, you can move your hand over the status bar to expand and process the news. Added Notification. Builder to make it easier to build notifications. Notification is a method that enables your application to run alert users without being enabled or in the background. It is the best way to warn users of events that need attention by invisible program components (Broadcast referers, services, and inactive activities.
First, differentiate the following status bar and status bar:

1. the status bar is a shape area at the top of the mobile phone screen;

There is a lot of information in the status bar, such as the usb connection icon, mobile phone signal icon, battery icon, and time icon;

2. the status bar is a scalable view that slides down from the status bar;

There are two types in the status bar (FLAG _ is used ):

(1) ongoing procedures;

(2) It is a notification event;

 

The procedure for quickly creating a Notification is as follows:

Step 1: Use the getSystemService () method to obtain the icationicationmanager object;

 

Java code
  1. NManager = (icationicationmanager) this. getSystemService (service );

Step 2: Set attributes of Notification, such as content, icon, title, and action of corresponding notification;

 

Java code
  1. Notification. icon = R. drawable. ic_launcher; // set the notification icon
  2. Notification. tickerText = tickerText; // text displayed in the status bar
  3. Notification. when = when; // set the time when the notification is sent.
  4. Notification. sound = Uri. parse ("android. resource: // com. sun. alex/raw/dida"); // custom sound
  5. Notification. flags = Notification. FLAG_NO_CLEAR; // The Message notification is cleared when you click the Clear button, but the Notification in the notification bar does not disappear.
  6. Notification. flags = Notification. FLAG_ONGOING_EVENT; // clicking the Clear button will not clear the Message notification, which can be used to indicate that the message is running
  7. Notification. flags | = Notification. FLAG_AUTO_CANCEL; // click the Clear button or the notification will automatically disappear.
  8. Notification. flags | = Notification. FLAG_INSISTENT; // always play, for example, keep playing the music and know the user response
  9. Notification. defaults = Notification. DEFAULT_SOUND; // calls the system's built-in sound.
  10. Notification. defaults = Notification. DEFAULT_VIBRATE; // sets the default vibration.
  11. Notification. defaults = Notification. DEFAULT_ALL; // set the ringtone to vibrate.
  12. Notification. defaults = Notification. DEFAULT_ALL; // set all attributes to the default value.

Step 3: Use icationicationmanager's notify () method to execute a notification message;

 

Java code
  1. NManager. Policy (ID, notification );

Step 4: Use the cancel () method of the icationicationmanager object to cancel a notificatioin message;

Java code
  1. NManager. cancel (ID );

Notification. build:

Void setLatestEventInfo (Context context, CharSequencecontentTitle, CharSequence contentText, PendingIntent contentIntent)

Function: display the Notification attribute in the stretch status bar. After you click it, The PendingIntent object is sent.

Parameter: context

Big title in contentTitle Status Bar

Title in the contentText Status Bar

After you click contentIntent, The PendingIntent object will be sent.

Note: If you add an icon to Notification, you must use this method to display the icon in the status bar and status bar. Otherwise, an error is returned!

 

Commonly used icationicationmanager class methods:

Obtain the object by getting the System Service:

Icationicationmanager mNotificationManager = (NotificationManager) getSystemService (Context. icationication_service );

 

Icationicationmanager common methods:

Public void cancelAll () removes all notifications (only for notifications under the current Context)

Public void cancel (int id) removes notifications marked as IDS (only for all notifications under the current Context)

Public void notify (String tag, int id, Notification notification) adds the Notification to the status bar. The label is tag and marked as id.

Public void y (int id, Notification notification) adds the Notification to the status bar and marks it as id

 

Java code
  1. Package com. sun. alex;
  2. Import android. app. Activity;
  3. Import android. app. Notification;
  4. Import android. app. icationicationmanager;
  5. Import android. app. PendingIntent;
  6. Import android. content. Intent;
  7. Import android.net. Uri;
  8. Import android. OS. Bundle;
  9. Import android. view. View;
  10. Import android. view. View. OnClickListener;
  11. Import android. widget. Button;
  12. Public class icationicationactivity extends Activity {
  13. Private Button sendBtn, cancelBtn;
  14. Private Notification notification;
  15. Private icationicationmanager nManager;
  16. Private Intent intent;
  17. Private PendingIntent pIntent;
  18. // Notification ID
  19. Private static final int ID = 1;
  20. @ Override
  21. Public void onCreate (Bundle savedInstanceState ){
  22. Super. onCreate (savedInstanceState );
  23. SetContentView (R. layout. main );
  24. SendBtn = (Button) this. findViewById (R. id. send );
  25. CancelBtn = (Button) this. findViewById (R. id. cancel );
  26. String service = icationication_service;
  27. NManager = (icationicationmanager) this. getSystemService (service );
  28. Notification = new Notification ();
  29. String tickerText = "test Notifaction"; // notification prompt
  30. // Display time
  31. Long when = System. currentTimeMillis ();
  32. Notification. icon = R. drawable. ic_launcher; // set the notification icon
  33. Notification. tickerText = tickerText; // text displayed in the status bar
  34. Notification. when = when; // set the time when the notification is sent.
  35. Notification. sound = Uri. parse ("android. resource: // com. sun. alex/raw/dida"); // custom sound
  36. Notification. flags = Notification. FLAG_NO_CLEAR; // The Message notification is cleared when you click the Clear button, but the Notification in the notification bar does not disappear.
  37. Notification. flags = Notification. FLAG_ONGOING_EVENT; // clicking the Clear button will not clear the Message notification, which can be used to indicate that the message is running
  38. Notification. flags | = Notification. FLAG_AUTO_CANCEL; // click the Clear button or the notification will automatically disappear.
  39. Notification. flags | = Notification. FLAG_INSISTENT; // always play, for example, keep playing the music and know the user response
  40. Notification. defaults = Notification. DEFAULT_SOUND; // calls the system's built-in sound.
  41. Notification. defaults = Notification. DEFAULT_SOUND; // sets the default ringtone.
  42. Notification. defaults = Notification. DEFAULT_VIBRATE; // sets the default vibration.
  43. Notification. defaults = Notification. DEFAULT_ALL; // set the ringtone to vibrate.
  44. Notification. defaults = Notification. DEFAULT_ALL; // set all attributes to the default value.
  45. SendBtn. setOnClickListener (sendClickListener );
  46. CancelBtn. setOnClickListener (cancelClickListener );
  47. }
  48. Private OnClickListener sendClickListener = new OnClickListener (){
  49. @ Override
  50. Public void onClick (View v ){
  51. // Click the notification to jump to the icationicationresult class.
  52. Intent = new Intent (icationicationactivity. this,
  53. Icationicationresult. class );
  54. // Get PendingIntent and send it when clicked
  55. PIntent = PendingIntent. getActivity (icationicationactivity. this, 0,
  56. Intent, 0 );
  57. // Set the notification title and content
  58. Notification. setLatestEventInfo (icationicationactivity. this, "title ",
  59. "Content", pIntent );
  60. // Send a notification
  61. NManager. Policy (ID, notification );
  62. }
  63. };
  64. Private OnClickListener cancelClickListener = new OnClickListener (){
  65. @ Override
  66. Public void onClick (View v ){
  67. // Cancel notification
  68. NManager. cancel (ID );
  69. }
  70. };
  71. }

Related Article

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.