The following is the first few sections of a chapter from The Busy Coder's Guide to Android Development, plus headings for the remaining major sections, to give you an idea about the content of the chapter.
Pop-up messages. Tray icons and their associated “bubble” messages. Bouncing dock icons. You are no doubt used to programs trying to get your attention, sometimes for good reason.
Your phone also probably chirps at you for more than just incoming calls: low battery, alarm clocks, appointment notifications, incoming text message or email, etc.
Not surprisingly, Android has a whole framework for dealing with these sorts of things, collectively called “notifications”.
Understanding this chapter requires you to have read the core chapters of the book.
A service, running in the background, needs a way to let users know something of interest has occurred, such as when email has been received. Moreover, the service may need some way to steer the user to an activity where they can act upon the event — reading a received message, for example. For this, Android supplies status bar icons, flashing lights, and other indicators collectively known as “notifications”.
Your current phone may well have such icons, to indicate battery life, signal strength, whether Bluetooth is enabled, and the like. With Android, applications can add their own status bar icons, with an eye towards having them appear only when needed (e.g., a message has arrived).
Notifications will appear in one of two places. On most devices, they will appear in the status bar, on the top of the screen, left-aligned:
Figure 405: Notifications, on a Galaxy Nexus
On a pre-Android 4.2 tablet (and occasionally on other tablets newer than that), they will appear in the system bar, on the bottom of the screen, towards the lower-right corner:
Figure 406: Notifications, on a Galaxy Tab 2
In either case, you can expand the “notification drawer” to get more details about the active notifications, either by sliding down the status bar:
Figure 407: Notification Drawer, on a Galaxy Nexus
or by tapping on the clock on the system bar on some tablets:
Figure 408: Notification Drawer, on a Galaxy Tab 2
Some notifications will be complex, showing real-time information, such as the progress of a long download. More often, notifications are fairly simple, providing just a couple of lines of information, plus an identifying icon. Tapping on the notification drawer entry will typically trigger some action, such as starting an activity — an email app letting the user know that “you’ve got mail” can have its notification bring up the inbox activity when tapped.
The preview of this section was whisked away by a shark-infested tornado.
The preview of this section left for Hollywood to appear in a reality TV show.
The preview of this section apparently resembled a Pokémon.
The preview of this section is en route to Mars.
The preview of this section was fed to a gremlin, after midnight.
The preview of this section is being chased by zombies.