Android Development notes (Notification)

Source: Internet
Author: User

Sometimes we need the app to display some notification information in the state, and then we need to use notification to do the work. Maybe we'll think of the toast we used to notify the user. Although notification and toast can serve as a notification, a reminder, in fact, these two still have a very big difference. In Android, notification is managed uniformly through notificationmanager, and toasts are displayed using Toast.maketoast. In addition, notification can be displayed for a long time in the system's status bar, but toast can only be temporarily displayed for a while.

Now let's talk about the general steps for creating notification.

First, we need to get a Notificationmanager object through the Getsystemservice method.

Notificationmanager Nfmanager = (notificationmanager) getsystemservice (Notification_service);

Then, to show a notification, we need a pendingintent, because Pendingintent is maintained by the Android system, which prevents the system from automatically destroying notification objects when the application shuts down.

Pendingintent Intent = pendingintent.getactivity (mainactivity.this, 0, Getintent (), 0);

Then, create a notification object from Notification.builder.

Notification Notification = new Notification.builder (mainactivity.this)             . Setsmallicon (Android. R.drawable.ic_dialog_email)//Set icon             . Setticker ("You have a new message")                        //Set scrolling notifications on receive             . Setcontenttitle ("Mail Subject: This is the first" + Notiid + "Mail")//Set the title             . Setcontenttext ("message content:")//Set message             . Setcontentintent (Intent)   // Set the intent             . Setnumber (NOTIID) for display          //Set the lower right corner number             . getnotification ();         API 11 can use Getnotificatin ()                                         //API 16 and then use Build () Notification.flags = Notification.flag_auto_cancel;      Click Auto Disappear

Finally, the notification can be displayed by Notificationmanager.

Nfmanager.notify (notiid++, notification);  NOTIID defines the number of the notification, notification the content of the notification

If you want to destroy the displayed notification you can use the method provided by Notificationmanager.

Nfmanager.cancel (NOTIID);  Destroy the notice Nfmanager.cancelall () numbered notiid;     Destroy all Notifications

The complete code is as follows:

Import Android.app.activity;import Android.app.notification;import Android.app.notificationmanager;import Android.app.pendingintent;import Android.os.bundle;import Android.view.view;import Android.widget.Button;public    Class Mainactivity extends Activity {private Notificationmanager nfmanager;    Private Button Btnnoti;    Private Button btnclear;        private int notiid;            @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);            Setcontentview (R.layout.activity_main);            NOTIID = 1;            Nfmanager = (Notificationmanager) getsystemservice (Notification_service);            Btnnoti = (Button) Findviewbyid (R.id.brnnoti); Btnnoti.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View V                    Iew) {Pendingintent intent = pendingintent.getactivity (mainactivity.this, 0, Getintent (), 0); Notification Notification = new Notification.builder (mainactivity.this). Setsmallicon (Android.                            R.drawable.ic_dialog_email)//Set icon. Setticker ("You have a new message")//Set scrolling notifications on receive . Setcontenttitle ("Mail Subject: This is the first" + Notiid + "mail")//set title. Setcontentte                            XT ("Message content:")//Set message. Setcontentintent (Intent)//Set Intent for display         . Setnumber (NOTIID)//Set the lower right corner number. GetNotification ();                    API 11 can use Getnotificatin ()//API 16 and later use Build ()      Notification.flags = Notification.flag_auto_cancel;                Click Auto Disappear nfmanager.notify (notiid++, notification);            }            });            Btnclear = (Button) Findviewbyid (r.id.btnclear); Btnclear.setonclicklistener (New View.onclicklistener () {@OvErride public void OnClick (view view) {Nfmanager.cancel (Notiid-);//                Nfmanager.cancelall ();    }            }); }}

Android Development Notes (Notification)

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.