"Sail Plan 024" 2015 Sail plan Android Apidemo Devil pace app->notification->incomingmessage status bar notifications

Source: Internet
Author: User

An application can use notifications to notify a user that an event has occurred (such as receiving a text message). Class Notificationmanager is used to handle notification, Notificationmanager can:

    • A new icon is displayed on the status bar.
    • Displays additional information on the Extended Status bar window or initiates an activity.
    • Displays the backlight/led.
    • Make the device vibrate.
    • Make a sound, etc.

For some application components that do not have a UI (such as broadcast Receiver, Services) or inactive activity,notification, it is recommended to use a method that alerts the user.

Notification usually displays an icon or text on the status bar, and if the user wants to know more about notification, you can press and hold the status bar drop-down to display the Expanded Status bar window, in expanded The Status Bar window displays the notification details and can initiate the corresponding activity.

The Incomingmessage example describes the general use of notification:

1. First, get the Notificationmanager object:

        // Look up the Notification Manager service        Notificationmanager nm = (Notificationmanager) getsystemservice (Notification_service);

2. Then create notification, specify the icon, text, and time to display the notification when notification is created:

        // construct the Notification object.        New Notification (R.drawable.stat_sample, Tickertext,                System.currenttimemillis ());

3. Then define the title and details when the user opens the Extented Status Windows window. Notification often represents a request or event that requires attention, so you can specify a pendingintent to respond to the user clicking on the notification.

        //The details of our fake messageCharsequence from = "Joe";        Charsequence message; Switch((NewRandom (). Nextint ())% 3) {             Case0:message = "R u hungry?" I am starved "; Break;  Case1:message = "im nearby U"; Break; default: Message = "KTHX." Meet u for dinner. Cul8r "; Break; }        //The pendingintent to launch we activity if the user selects this//notification. Note the use of flag_cancel_current so, if there//is already an active matching pending intent, cancel it and replace//it with the new array of Intents.Pendingintent contentintent = pendingintent.getactivities ( This, 0, makemessageintentstack( This, from , message), pendingintent.flag_cancel_current); //the ticker text, this uses a formatted string so we have a message could be localizedString Tickertext =getString (r.string.imcoming_message_ticker_text, message); //construct the Notification object.Notification Notif =NewNotification (R.drawable.stat_sample, Tickertext, System.currenttimemillis ()); //Set the info for the "that" show in the notification panel.        Notif.setlatesteventinfo( This, from, message, contentintent); //We'll have this notification does the default sound, vibration, and LEDs. //Note If you want any of the these behaviors, you should always have//a preference for the user to turn them off.        Notif.defaults=Notification.default_all;

4. Finally, this notification is triggered:

        // Note that we use R.layout.incoming_message_panel         as the ID for // The notification.  It could is any        integer want, but weuse// The Convention of using a resource ID for a string R Elated        to// the notification.  It always is a unique number        within your//  application.        Nm.notify (R.string.imcoming_message_ticker_text, Notif);

In general, the same event can be used to notify the user of the same notification, the first parameter of nm.notify is the ID of notification, and the type is an integer. You can use the same ID to represent the same notification, or you can use this ID to cancel the notification, and in Incomingmessage, when the user clicks on the incomingmessage details, This notification (in class Incomingmessageview) is canceled.

Nm.cancel (R.string.imcoming_message_ticker_text);  

"Sail Plan 024" 2015 Sail plan Android Apidemo Devil pace app->notification->incomingmessage status bar notifications

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.