Android Series Lectures (1): Notification and status bar Information

Source: Internet
Author: User
This article is original. If you need to reprint it, please indicate the author and the source. Thank you!

NotificationAndToastNotifications and reminders. However, their implementation principles and forms are completely different.ToastIn fact, it is equivalent to a component (Widget). Some dialog boxes are similar to those without buttons. WhileNotificationIs the information displayed in the status bar above the screen. There isNotificationRequiredIcationicationmanagerTo manage, andToastSimply createToastObject.

Next let's take a look at creating and displayingNotification. Create and displayNotificationThe following is required:5Step:

1.PassGetsystemserviceMethod to obtainIcationicationmanagerObject.

2.CreateNotificationObject. EveryNotificationCorresponds toNotificationObject. In this step, you need to set the image resources in front of the notification message and notification message displayed in the status bar above the screen.IDAnd the time when the notification was sent. It is generally the current time.

3.BecauseNotificationApplicable to applicationsProgram. That is, even if the application is closed,NotificationIt is still displayed in the status bar. After the application is started again, you can re-control theseNotification. Such as clearing or replacing them. Therefore, you must createPendingintentObject. This object is composedAndroidThe system is responsible for maintenance. Therefore, the object will not be released after the application is closed.

4.UseNotificationClassSetlatesteventinfoMethod settingsNotification.

5.UseIcationicationmanagerClassNotifyMethod displayNotificationMessage. In this step, you need to specify the identifierNotificationUniqueID. ThisIDMust be relative to the sameIcationicationmanagerThe object is unique, otherwise it will overwrite the sameIDOfNotificaiton.

It's not as exciting as action. Let's show you how to displayNotification,CodeAs follows:

// Step 2
Icationicationmanager notificationmanager = (Icationicationmanager) getsystemservice (notification_service );
// Step 2
Notification = New Notification (R. drawable. Icon, " You have a new message " , System. currenttimemillis ());
// Step 2
Pendingintent contentintent = Pendingintent. getactivity ( This , 0 , Getintent (), 0 );
// Step 2
Notification. setlatesteventinfo ( This , " Weather Forecast " , " Clear to cloudy " , Contentintent );
// Step 2
Icationicationmanager. Y (R. drawable. Icon, notification );

Use the following codeShownotificationThe method is displayed3ItemsNotificationMessage.

Shownotification ( " Very happy today " , " I got the first grade in the test today. " ,
" 100 in mathematics, 99 in Chinese, 100 in English, yeah! " , R. drawable. Smile, R. drawable. Smile );
Shownotification ( " Why? " , " Why is this question wrong? " , " Who has the correct answer. " ,
R. drawable. Why, R. drawable. Why );
Shownotification ( " Bad mood today " , " I don't know why. These days have been depressing. " , " Maybe I should go to the park to relax. " ,
R. drawable. Why, R. drawable. Wrath );

The2And3ItemsNotificationUse the sameID(R. drawabgle. Why). Therefore3ItemsNotificationWill overwrite2ItemsNotification.

On displayNotificationYou can also set the default voice, vibration, andLightEffect. To implement this function, you need to setNotificationClassUltsThe Code is as follows:

Notification. defaults = Notification. default_sound; // Use default sound
Notification. defaults = Notification. default_vibrate; // Use the default Vibration
Notification. defaults = Notification. default_lights; // Use the default light
Notification. defaults = Notification. default_all; // All use default values

Note: Set the default voice, vibration, and light . This method is similar to shownotification the implementation code of this method is basically the same, only when calling notify before the method, you must set defaults attribute ( defaults the property must be called before notify method, otherwise it does not work ). When setting the default vibration effect, you also need to androidmanifest. XML file tag Settings android. permission. vibrate permission.

to clear a message, use icationicationmanager cancel This method has only one parameter, ID of notification . Use cancelall to clear the current icationicationmanager all notification In the object.

Run the example in this section and click display on the screenNotificationAs shown in figure 1.Message. Each message is displayed for a while, and then only the entire message is displayed.AndroidSystem (including other applications)Notification(Only the image part is displayed ). 2. If you drag the status bar, you can seeNotificationAnd the time when the notification was issued (that isNotificationThe3Parameter values), 3As shown in. When you click the clear notification button, allNotification, After clearing effect 4.

Figure 1


Figure 2


Figure 3


Figure 4

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.