Android Programming Tutorials

(Romina) #1
A Subtle Notification

The Intent has a pair of flags added, FLAG_ACTIVITY_CLEAR_TOP and


FLAG_ACTIVITY_SINGLE_TOP. The combination of these means that if there is a


copy of Patchy presently active, it will be brought to the foreground, rather


than start a new copy of Patchy.


To use this new method, you will also need to declare the NOTIFICATION_ID


value in PostMonitor:


public static final int NOTIFICATION_ID= 1337 ;

You will also need to add imports for android.app.Notification,


android.app.NotificationManager, and android.app.PendingIntent.


Step #3: Watch for the Keyword........................................................


Armed with the implementation of showNotification(), we can now modify


the poll() method to watch for hits on the keyword, so make your


implementation of poll() in PostMonitor look like this:


private void poll(Account l) {
try {
Twitter client=new Twitter(l.user, l.password);

client.setAPIRootUrl("https://identi.ca/api");

List<Twitter.Status> timeline=client.getFriendsTimeline();

for (Twitter.Status s : timeline) {
if (!seenStatus.contains(s.id)) {
l.callback.newFriendStatus(s.user.screenName, s.text,
s.createdAt.toString());
seenStatus.add(s.id);

if (s.text.indexOf(NOTIFY_KEYWORD)>- 1 ) {
showNotification();
}
}
}
}
catch (Throwable t) {
android.util.Log.e("PostMonitor",
"Exception in poll()", t);
}
}

221
Free download pdf