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
- NManager = (icationicationmanager) this. getSystemService (service );
Step 2: Set attributes of Notification, such as content, icon, title, and action of corresponding notification;
Java code
- Notification. icon = R. drawable. ic_launcher; // set the notification icon
- Notification. tickerText = tickerText; // text displayed in the status bar
- Notification. when = when; // set the time when the notification is sent.
- Notification. sound = Uri. parse ("android. resource: // com. sun. alex/raw/dida"); // custom sound
- 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.
- 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
- Notification. flags | = Notification. FLAG_AUTO_CANCEL; // click the Clear button or the notification will automatically disappear.
- Notification. flags | = Notification. FLAG_INSISTENT; // always play, for example, keep playing the music and know the user response
- Notification. defaults = Notification. DEFAULT_SOUND; // calls the system's built-in sound.
- Notification. defaults = Notification. DEFAULT_VIBRATE; // sets the default vibration.
- Notification. defaults = Notification. DEFAULT_ALL; // set the ringtone to vibrate.
- 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
- NManager. Policy (ID, notification );
Step 4: Use the cancel () method of the icationicationmanager object to cancel a notificatioin message;
Java code
- 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
- Package com. sun. alex;
- Import android. app. Activity;
- Import android. app. Notification;
- Import android. app. icationicationmanager;
- Import android. app. PendingIntent;
- Import android. content. Intent;
- Import android.net. Uri;
- Import android. OS. Bundle;
- Import android. view. View;
- Import android. view. View. OnClickListener;
- Import android. widget. Button;
- Public class icationicationactivity extends Activity {
- Private Button sendBtn, cancelBtn;
- Private Notification notification;
- Private icationicationmanager nManager;
- Private Intent intent;
- Private PendingIntent pIntent;
- // Notification ID
- Private static final int ID = 1;
- @ Override
- Public void onCreate (Bundle savedInstanceState ){
- Super. onCreate (savedInstanceState );
- SetContentView (R. layout. main );
- SendBtn = (Button) this. findViewById (R. id. send );
- CancelBtn = (Button) this. findViewById (R. id. cancel );
- String service = icationication_service;
- NManager = (icationicationmanager) this. getSystemService (service );
- Notification = new Notification ();
- String tickerText = "test Notifaction"; // notification prompt
- // Display time
- Long when = System. currentTimeMillis ();
- Notification. icon = R. drawable. ic_launcher; // set the notification icon
- Notification. tickerText = tickerText; // text displayed in the status bar
- Notification. when = when; // set the time when the notification is sent.
- Notification. sound = Uri. parse ("android. resource: // com. sun. alex/raw/dida"); // custom sound
- 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.
- 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
- Notification. flags | = Notification. FLAG_AUTO_CANCEL; // click the Clear button or the notification will automatically disappear.
- Notification. flags | = Notification. FLAG_INSISTENT; // always play, for example, keep playing the music and know the user response
- Notification. defaults = Notification. DEFAULT_SOUND; // calls the system's built-in sound.
- Notification. defaults = Notification. DEFAULT_SOUND; // sets the default ringtone.
- Notification. defaults = Notification. DEFAULT_VIBRATE; // sets the default vibration.
- Notification. defaults = Notification. DEFAULT_ALL; // set the ringtone to vibrate.
- Notification. defaults = Notification. DEFAULT_ALL; // set all attributes to the default value.
- SendBtn. setOnClickListener (sendClickListener );
- CancelBtn. setOnClickListener (cancelClickListener );
- }
- Private OnClickListener sendClickListener = new OnClickListener (){
- @ Override
- Public void onClick (View v ){
- // Click the notification to jump to the icationicationresult class.
- Intent = new Intent (icationicationactivity. this,
- Icationicationresult. class );
- // Get PendingIntent and send it when clicked
- PIntent = PendingIntent. getActivity (icationicationactivity. this, 0,
- Intent, 0 );
- // Set the notification title and content
- Notification. setLatestEventInfo (icationicationactivity. this, "title ",
- "Content", pIntent );
- // Send a notification
- NManager. Policy (ID, notification );
- }
- };
- Private OnClickListener cancelClickListener = new OnClickListener (){
- @ Override
- Public void onClick (View v ){
- // Cancel notification
- NManager. cancel (ID );
- }
- };
- }