Android notification and toast (1)

Source: Internet
Author: User

Both notification and toast can serve as notifications and reminders. However, their implementation principles and forms are completely different. Toast is actually equivalent to a widget ). Some dialog boxes are similar to those without buttons. Notification is the information displayed in the status bar above the screen. In addition, notification needs to be managed by icationicationmanager, and toast only needs to be created simply.

The following describes how to create and display a notification. To create and display a notification, follow these steps:

1. Get a icationicationmanager object through the getsystemservice method.
2. Create a notification object. Each notification corresponds to a notification object. In this step, you need to set the notification message displayed on the status bar above the screen, the image resource ID in front of the notification message, and the time when the notification is sent. It is generally the current time.
3. Notification can be detached from the application. That is to say, even if the application is closed, notification will still be displayed in the status bar. After the application is started again, you can re-control these notifications. Such as clearing or replacing them. Therefore, you must create a pendingintent object. This object is maintained by the Android system. Therefore, the object will not be released after the application is closed.
4. Use the setlatesteventinfo method of the notification class to set notification details.
5. Use the y icationmanager class's notify y method to display notification messages. In this step, you must specify the unique ID that identifies the notification. This ID must be unique relative to the same notificationmanager object. Otherwise, it will overwrite the same icationicaiton object.
It's not as exciting as action. Let's demonstrate how to display a notification in the status bar. The Code is as follows:

Java code:

  1. // Step 2
  2. Icationicationmanager notificationmanager = (notificationmanager) getsystemservice (notification_service );
  3. // Step 2
  4. Notification = new notification (R. drawable. Icon, "you have a new message", system. currenttimemillis ());
  5. // Step 2
  6. Pendingintent contentintent = pendingintent. getactivity (this, 0, getintent (), 0 );
  7. // Step 2
  8. Notification. setlatesteventinfo (this, "Weather Forecast", "Clear to cloudy", contentintent );
  9. // Step 2
  10. Icationicationmanager. Y (R. drawable. Icon, notification );
  11. Shownotification ",
  12. "Mathematics 100, Chinese 99, English 100, yeah! ", R. drawable. Smile, R. drawable. Smile );
  13. Shownotification ("why? "," Why is this question wrong? "," Who has the correct answer .",
  14. R. drawable. Why, R. drawable. Why );
  15. Shownotification ("bad mood today", "I don't know why, these days have been very depressing.", "Maybe I should go to the park to relax ",
  16. R. drawable. Why, R. drawable. Wrath );

Copy code



Series of Android notification and toast (ii) Post link http://www.eoeandroid.com/thread-81330-1-1.html

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.