Android Programming Tutorials

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

public void onServiceDisconnected(ComponentName className) {
service=null;
}
};

Notice how it will obtain our IPostMonitor API via the "binder" object


passed to it – since this is a local service, in the same VM, this is the actual


LocalBinder object from PostMonitor. Also note that we register our account


information and an IPostListener instance at this point.


We need to unbind our service, after removing our account, when we are


done, so add an implementation of onDestroy() to Patchy as follows:


@Override
public void onDestroy() {
super.onDestroy();

service.removeAccount(listener);
unbindService(svcConn);
}

Our code in onCreate() refers to a listener instance, so add a definition of it


as an IPostListener implementation to Patchy:


private IPostListener listener=new IPostListener() {
public void newFriendStatus(String friend, String status,
String createdAt) {
}
};

Right now, this listener does not do anything – we will address that minor


shortcoming in the next section.


At this point, we register our account at startup and remove it on


shutdown. What we are missing is account changes. If the user, via the


preferences, adjusts the user name or password, we need to get that


information to the service. The cleanest way to do that is to remove our


current account and register a fresh one.


With that in mind, modify resetClient() to handle that chore:


187
Free download pdf