Seeing Pestering in Action
Let us now take a peek at the sample project (available in the Source Code section at http://apress.com), in particular the class:
Public class extends
Private static final
Private new Timer
Private
Public onCreate
Super onCreate
SetContentView
FindViewById
SetOnClickListener new OnClickListener
Public onClick
New TimerTask
Public run
NotifyMe
Schedule
FindViewById
SetOnClickListener new OnClickListener
Public onClick
GetSystemService
Cancel
Private notifyMe
Final
GetSystemService
New Notification
CurrentTimeMillis
GetActivity this
New Intent this class
SetLatestEventInfo this
Notify
This activity sports two large buttons, one to kick off a notification after a five‑second delay, and one to cancel that notification (if it is active). See Figure 32‑1.
Figure 32‑1. The NotifyDemo activity main view
Creating the notification, in , is accomplished in five steps:
1. Get access to the instance.
2. Create a object with our icon (red ball), a message to flash on the status bar as the notification is raised, and the time associated with this event.
3. Create a that will trigger the display of another activity ().
4. Use to specify that, when the notification is clicked on, we are to display a certain title and message, and if that is clicked on, we launch the .
5. Tell the to display the notification.
Hence, if we click the top button, after five seconds our red ball icon will appear in the status bar. Our status message will also appear briefly, as shown in Figure 32‑2.
Figure 32‑2. Our notification as it appears on the status bar, with our status message
If you click on the red ball, a drawer will appear beneath the status bar. Drag that drawer all the way to the bottom of the screen to show the outstanding notifications, including our own, as shown in Figure 32‑3.
Figure 32‑3. The notifications drawer, fully expanded, with our notification
If you click on the notification entry in the drawer, you’ll be taken to a trivial activity displaying a message – though in a real application this activity would do something useful based upon the event that occurred (e.g., take users to the newly arrived mail messages).
Clicking on the cancel button, or clicking on the Clear Notifications button in the drawer, will remove the red ball from the status bar.
PART 6
Дата добавления: 2015-05-16; просмотров: 867;