Android Programming Tutorials

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

Step #2: Enable Callbacks in the Service..........................................


Now we need to set up a means for the service client (Patchy) to start the


service and register an IPostListener that will be invoked when a new status


arrives. The callback will be associated with a user name and password, so


we have the credentials with which to call identi.ca via its API.


First, let us create an inner class in PostMonitor to represent the callback


and login credentials. Add this implementation of Account to your


PostMonitor:


class Account {
String user=null;
String password=null;
IPostListener callback=null;

Account(String user, String password,
IPostListener callback) {
this.user=user;
this.password=password;
this.callback=callback;
}
}

Next, import java.util.Map and java.util.concurrent.ConcurrentHashMap and


set up a private data member representing the roster of outstanding


accounts:


private Map<IPostListener, Account> accounts=
new ConcurrentHashMap<IPostListener, Account>();

Now, we can set up public APIs on our binder for our client to use to


register and remove an account, by extending IPostMonitor:


package apt.tutorial;

import apt.tutorial.IPostListener;

public interface IPostMonitor {
void registerAccount(String user, String password,
IPostListener callback);
void removeAccount(IPostListener callback);
}

184
Free download pdf