Android Programming Tutorials

(Romina) #1
No, Really Listening To Your Friends

...and by augmenting its implementation:


public class LocalBinder extends Binder implements IPostMonitor {
public void registerAccount(String user, String password,
IPostListener callback) {
Account l=new Account(user, password, callback);

poll(l);
accounts.put(callback, l);
}

public void removeAccount(IPostListener callback) {
accounts.remove(callback);
}
}

Notice that we call poll() with our Account when it is registered. This will


fetch our current timeline right away, rather than wait for our polling loop


to cycle back around. The downside is that this poll() is called on the main


thread rather than a background thread – we will address this in a later


tutorial.


Next, update our threadBody to make use of our roster of Account objects:


private Runnable threadBody=new Runnable() {
public void run() {
while (active.get()) {
for (Account l : accounts.values()) {
poll(l);
}

SystemClock.sleep(POLL_PERIOD);
}
}
};

This too calls poll() with the Account information. Hence, we need to


update poll() to use the Account and call the callback on new status


messages:


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

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

185
Free download pdf