Android Programming Tutorials

(Romina) #1
Messages From The Great Beyond

names for any values we are going to store as "extras" in our Intent, and


ideally those are "namespaced" as well.


With that in mind, add the following data members to PostMonitor:


public static final String STATUS_UPDATE="apt.tutorial.three.STATUS_UPDATE";
public static final String FRIEND="apt.tutorial.three.FRIEND";
public static final String STATUS="apt.tutorial.three.STATUS";
public static final String CREATED_AT="apt.tutorial.three.CREATED_AT";

Then, we need to actually broadcast the Intent, instead of calling the


callback. To do that, replace your existing poll() implementation with the


following:


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)) {
try {
Intent broadcast=new Intent(STATUS_UPDATE);
broadcast.putExtra(FRIEND, s.user.screenName);
broadcast.putExtra(STATUS, s.text);
broadcast.putExtra(CREATED_AT,
s.createdAt.toString());
sendBroadcast(broadcast);
}
catch (Throwable t) {
Log.e("PostMonitor", "Exception in callback", t);
}

seenStatus.add(s.id);

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

272
Free download pdf