Android status bar notification notification, Notificationmanager introduction

Source: Internet
Author: User

Notification (notice) generally used in the phone, sms, mail, Alarm clock, in the status bar of the phone will appear a small icon, prompting the user to handle this notification, then the hand from the top of the slide status bar can be expanded and processed this notice;

In the Android system, it is convenient to send a status bar notification. Send a status bar notification must use two classes: Notificationmanager, Notification;

Notificationmanager: is the status bar notification management class, responsible for the notification, clear notification, etc. notificationmanager is a system service that must begetSystemService()方法来获取;

Notification: is the specific status bar notification object, you can set icon, text, cue sound, vibration and so on parameters;

Let's look at the basic parameters required for the next notification:

(1) Icon: icons;

(2) Pendingintent: Click the notification execution page to jump;

(2) Ticker text:notification just out of the time, in the status bar scrolling subtitles, if very long, will automatically split the scroll;

(3) The title of Content Title:notification after its expansion;
(4) Content text:notification after the expansion;

A notification usually requires the following steps:

(1) Get Notificationmanager:

Nmanager = (Notificationmanager) getsystemservice (Notification_service);

(2) Instantiate the notification object and set the properties of the notification;

Notification.icon = R.drawable.icon;//set the icon that the notification displays in the status barNotification.tickertext = "one Message is coming!!!";//notification of what is displayed in the status barNotification.when = when;//set the time to notifyNotification.sound = Uri.parse ("Android.resource://com.sun.alex/raw/dida");//Custom SoundsNotification.flags |= Notification.flag_auto_cancel;//clicking the Clear button or clicking the notification will automatically disappearNotification.flags |= notification.flag_insistent;//All the time , like music always playing, knowing the user responseNotification.defaults = Notification.default_sound;//call system comes with soundNotification.defaults = notification.default_vibrate;//Set Default VibrationNotification.defaults = Notification.default_all;//set the ringtone to vibrateNotification.defaults = Notification.default_all;//set all properties to the default

(3) Call the Setlatesteventinfo () method to set the icon and time in the view;

// Instantiate Intent New Intent (mainactivity.  this, mainactivity. class ); // Get Pendingintent Pendingintent pintent = pendingintent.getactivity (mainactivity.  this, 00); // Set Event information notification.setlatesteventinfo (mainactivity.  this, "Title", "Content",pintent);

(4) Notification: A notification message is executed by the Notify () method of the Notificationmanager object;

Private int notification_id =nmanager.notify (notification_id, Notification);

(5) Clear notification: Through the Notificationmanager cancel (int) method, to clear a notification, where the parameter is the unique identification ID of notification, of course, can also be cancelall () to clear the status bar all notifications;

Nmanager.cancel (notification_id);

Here's an example to illustrate:

Three buttons, respectively, are sent, then updated, the last clear notification;

The first step, click Send after;

Then check the notification:

In the second step, click Update Notifications, and then view the notifications:

Step three: Clear notifications:

The implementation code is as follows:

Layout file Activity_main.xml only three buttons, do not write;

mainly see Mainactivity.java

 Packagecom.xiaozhang.notificationtest;Importandroid.app.Activity;Importandroid.app.Notification;ImportAndroid.app.NotificationManager;Importandroid.app.PendingIntent;Importandroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button; Public classMainactivityextendsActivity {PrivateButton button1, Button2, Button3; //Notification Manager    PrivateNotificationmanager Nmanager; //Notification Display content    Privatependingintent pendingintent; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main);    Init (); }    Private voidinit () {button1=(Button) Findviewbyid (R.id.button1); Button2=(Button) Findviewbyid (R.id.button2); Button3=(Button) Findviewbyid (R.id.button3);        Button1.setonclicklistener (onclick);        Button2.setonclicklistener (onclick);        Button3.setonclicklistener (onclick); Nmanager=(Notificationmanager) Getsystemservice (Notification_service); Intent Intent=NewIntent ( This, Mainactivity.class); Pendingintent= Pendingintent.getactivity (mainactivity. This, 0, Intent,0); } onclicklistener onclick=NewOnclicklistener () {Private intnotification_id = 110; PrivateNotification Notification; @SuppressWarnings ("Deprecation") @Override Public voidOnClick (View v) {Switch(V.getid ()) { Caser.id.button1:notification=NewNotification (); Notification.icon= R.drawable.icon;//set the icon that the notification displays in the status barNotification.tickertext = "one Message is coming!!!";//notification of what is displayed in the status bar//If you want to take all of the default values, use Default_all. //The default sound is used hereNotification.defaults |=Notification.default_sound; Notification.defaults|=notification.default_vibrate; Notification.defaults|=notification.default_lights; //let the sound and vibration loop indefinitely until the user respondsNotification.flags |=notification.flag_insistent; //automatically disappears when notifications are clickedNotification.flags |=Notification.flag_auto_cancel; //Second parameter: message caption displayed when drop-down status bar//third parameter: message content displayed when drop-down status bar//Fourth parameter: Perform a page jump when you click on the notificationNotification.setlatesteventinfo (mainactivity. This, "Notification 1",                        "First message: One message", pendingintent); //Issue status bar notificationsnmanager.notify (notification_id, Notification);  Break;  CaseR.id.button2://Update Notifications//For example, the status bar prompt has a new text message, haven't had time to check, and a new message prompt, you can update the original notification method. //(It is also possible to resend a notification, but this will cause confusion in the notification, and display multiple notifications to the user, not user friendly)Notification.setlatesteventinfo (mainactivity. This, "Notification 2",                        "Second message: Second message", pendingintent);                Nmanager.notify (notification_id, Notification);  Break;  CaseR.id.button3:nmanager.cancel (notification_id);  Break; }        }    };}

Android status bar notification notification, Notificationmanager introduction

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.