Android Tutorial

(avery) #1
Android Tutorial 413

 An intent (launches if the user clicks on the full notification)

In this section, you learn how to create this basic kind of
notification.

Using the NotificationManager Service

All notifications are created with the help of the
NotificationManager. The Notification Manager (within the
android.app package) is a system service that must be requested.
The following code demonstrates how to obtain a valid
NotificationManager object using the getSystemService() method:

NotificationManager notifier = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);


The NotificationManager is not useful without having a valid
Notification object to use with the notify() method. The Notification
object defines what information displays to the user when the
Notification is triggered. This includes text that displays on the
status bar, a couple of lines of text that display on the expanded
status bar, an icon displayed in both places, a count of the number
of times this Notification has been triggered, and a time for when
the last event that caused this Notification took place.

Creating a Simple Text Notification with an Icon

You can set the icon and ticker text, both of which display on the
status bar, through the constructor for the Notification object, as
follows:

Notification notify = new Notification(
R.drawable.android_32, "Hello!", System.currentTimeMillis());

Free download pdf