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:
- // Step 2
- Icationicationmanager notificationmanager = (notificationmanager) 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 );
- Shownotification ",
- "Mathematics 100, Chinese 99, English 100, 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 very depressing.", "Maybe I should go to the park to relax ",
- 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