Android Programming Tutorials

(Romina) #1
Your Friends Seem Remote

The easiest way to make the above change is to change the first line of the


LocalBinder definition to the first line in the above listing, add the


semicolon at the end, and get rid of the separate declaration and initializer


of binder.


Then, we need to do something about our polling logic, so when we register


an account we do not tie up the UI thread of the client doing an immediate


poll(), yet we still quickly get status updates rather than waiting a full


minute. To help resolve this, we will use two polling periods: a one-second


period when we have no accounts, and the original one-minute period for


when we do have accounts. That way, we will find out about our first


account within one second of it being registered; only then will we slow


down, so as not to abuse the service.


To that end, add the following data members to PostMonitor:


private static final int INITIAL_POLL_PERIOD= 1000 ;
private int pollPeriod=INITIAL_POLL_PERIOD;

Also, change threadBody to use the new polling logic:


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

SystemClock.sleep(pollPeriod);
}
}
};

Now, you should be able to recompile and reinstall both projects.


Launching Patchy will give you similar results as before – the difference


being that now it is using the remote PostMonitor service, rather than a local


one.


Here is an implementation of Patchy after making this tutorial's


modifications:


209
Free download pdf