Learn Android-notification with me.

Source: Internet
Author: User

Notification can be understood as notification meaning, will appear in the notification bar, such as a text message

There are 3 steps to using the Notification:

1. Create an Notificationmanager object

2. Setting properties for notification

3. Send notifications using notify provided by Notificationmanager

Example: Issue a notification

1     /**2 * Create notify3      */4     Private voidcreatenotify () {5         //Create a Notificationmanager object6Notificationmanager Manager =(Notificationmanager) Getsystemservice (notification_service);7         //Create a Notifycation object8Notification notify =NewNotification ();9Notify.icon = r.drawable.icon_reply;//Set iconTenNotify.when = System.currenttimemillis ();//time of notification, immediately OneNotify.tickertext = "Hi, here I Come";//hint Text ANotify.flags = Notification.flag_auto_cancel;//automatically cancel after user clicks -Intent Intent =NewIntent ( This, Nextactivity.class); -Pendingintent pintent = pendingintent.getactivity ( This, 0, Intent, the pendingintent.flag_update_current); -Notify.setlatesteventinfo ( This, "Come to the News," "a Notice.", pintent); -Manager.notify (Ten, notify);//Notification , 10 is the ID of the notification -}

This method you can set is the button of the event call, after running the program, click on the button to see the notification sent out. Layout files and related code are not written here.

Pendingintent for Intent packaging, here is a description of the start intent, pendingintent.getactivity returns the pendingintent expression

The intent in this pendingintent instance is the intent used to start the Activity.

The parameters of the pendingintent.getactivity are: Context, the sender's request code (can fill 0), for the system to send the intent, flag bit.

Where pendingintent.flag_update_current indicates that if the pendingintent of the description already exists, The extra data for the existing pendingintent is changed to the extra data for the new pendingintent.

The difference between Intent and pendingintent:

Intent: The intention, that is to tell the system what I want to do, and then the system according to this Intent do the corresponding thing. such as startactivity is equivalent to sending a message, while intent is the content of the message.

Pendingintent: Packaging Intent,intent is our intention to use startactivity, StartService, or sendbroadcast to start a work directly.

At some point, we can not call startactivity, startservide or sendbroadcast directly, but send intent when a program or system reaches a certain condition.

As notification here, when the user clicks Notification, the system issues an activity Intent. So if we don't use some sort of method to tell the system, the system doesn't know it's using startactivity,

StartService or Sendbroadcast to start intent (of course, there are other "descriptions"), so there is a need for pendingintent.

Learn Android-notification with me.

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.