Android Programming Tutorials

(Romina) #1
A Subtle Notification

something suitable from the Android SDK (look for status_* icons), draw


one yourself, or use the one supplied in the source code for this book.


Step #2: Raise the Notification..........................................................


Next, we need to add a method to PostMonitor that will use the


NotificationManager and raise a Notification. Add the following


showNotification() method to PostMonitor:


private void showNotification() {
final NotificationManager mgr=
(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification note=new Notification(R.drawable.status,
"New matching post!",
System.currentTimeMillis());
Intent i=new Intent(this, Patchy.class);

i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|
Intent.FLAG_ACTIVITY_SINGLE_TOP);

PendingIntent pi=PendingIntent.getActivity(this, 0 ,
i,
0 );

note.setLatestEventInfo(this, "Identi.ca Post!",
"Found your keyword: "+NOTIFY_KEYWORD,
pi);

mgr.notify(NOTIFICATION_ID, note);
}

Here we get access to the NotificationManager via getSystemService(), create


an configure a Notification object, and tell the NotificationManager to show


the Notification.


The one part of this that is a bit tricky is the work with the Intent and


PendingIntent. A PendingIntent is a wrapper around an Intent, stating how it


should be used (in our case, to start an Activity) and allowing some other


process to execute this Intent using our security profile. In this case, this


means that the operating system will call startActivity() on the Intent


using the Patchy application's set of permissions, not the permissions of the


operating system itself.


220
Free download pdf