Android Programming Tutorials

(Romina) #1
Listening To Your Friends

Then, in our threadBody loop, add a call to a poll() method:


private Runnable threadBody=new Runnable() {
public void run() {
while (active.get()) {
poll();
SystemClock.sleep(POLL_PERIOD);
}
}
};

Finally, add this as the implementation of poll() in PostMonitor:


private void poll() {
Twitter client=new Twitter(); // need credentials!
List<Twitter.Status> timeline=client.getFriendsTimeline();
}

While this will compile, it will not run, since we do not have our user name


or password. We will get that from the Patchy client of our service in the


next tutorial.


Right now, all we are doing in poll() is creating a fresh Twitter object and


using it to load the timeline via getFriendsTimeline().


Step #4: Find New Statuses................................................................


Once we have our timeline, we need to figure out which of the statuses are


new. On the first poll, all will be new; subsequent polls should have a


handful of new statuses, if any.


For the time being, let us track the already-seen status IDs via a Set,


so add the following data member (and the HashSet and Set imports to


match):


private Set<Long> seenStatus=new HashSet<Long>();

Then, we can walk the timeline, see if each status ID is in the Set, and


process those that are not, via this change to poll():


180
Free download pdf