: Beginning Android

Seeing Pestering in Action

Seeing Pestering in Action

Let us now take a peek at the Notifications/Notify1 sample project (available in the Source Code section athttp://apress.com), in particular the NotifyDemo class:

publicclass NotifyDemoextends Activity {
privatestaticfinal int NOTIFY_ME_ID = 1337;
private Timer timer =newTimer();
private int count = 0;
public voidonCreate(Bundle savedInstanceState) {
Button btn = (Button)findViewById(R.id.notify);
btn.setOnClickListener(new View.OnClickListener() {
public voidonClick(View view) {
TimerTask task =newTimerTask() {
public voidrun() {
timer.schedule(task, 5000);
btn = (Button)findViewById(R.id.cancel);
btn.setOnClickListener(new View.OnClickListener() {
public voidonClick(View view) {
NotificationManager mgr =
private voidnotifyMe() {
final NotificationManager mgr =
Notification note = new Notification(R.drawable.red_ball,
"Status message!", System.currentTimeMillis());
PendingIntent i = PendingIntent.getActivity(this, 0,
newIntent(this, NotifyMessage.class), 0);
note.setLatestEventInfo(this, "Notification Title",
"This is the notification message", i);
note.number = ++count;
mgr.notify(NOTIFY_ME_ID, note);

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 notifyMe(), is accomplished in five steps:

1.Get access to the NotificationManager instance.

2.Create a Notification 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 PendingIntent that will trigger the display of another activity (NotifyMessage).

4.Use setLatestEventInfo() 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 PendingIntent.

5.Tell the NotificationManager 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, youll 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.

: 1.487. /Cache: 3 / 0