Android Programming The Big Nerd Ranch Guide by Bill Phillips, Chris Stewart, Kristin Marsicano (z-lib.org)

(gtxtreme123) #1
Looking for New Results

Return to PollService.java and put this plan into action. Listing 28.7 shows a long swath of code,
but it uses nothing you have not seen before.


Listing 28.7  Checking for new results (PollService.java)


public class PollService extends IntentService {
private static final String TAG = "PollService";
...
@Override
protected void onHandleIntent(Intent intent) {
...
Log.i(TAG, "Received an intent: " + intent);
String query = QueryPreferences.getStoredQuery(this);
String lastResultId = QueryPreferences.getLastResultId(this);
List items;


if (query == null) {
items = new FlickrFetchr().fetchRecentPhotos();
} else {
items = new FlickrFetchr().searchPhotos(query);
}


if (items.size() == 0) {
return;
}


String resultId = items.get(0).getId();
if (resultId.equals(lastResultId)) {
Log.i(TAG, "Got an old result: " + resultId);
} else {
Log.i(TAG, "Got a new result: " + resultId);
}


QueryPreferences.setLastResultId(this, resultId);
}
...
}


See each part we discussed above? Good.


Run PhotoGallery, and you should see your app getting new results initially. If you have a search query
selected, you will probably see stale results when you subsequently start up the app.

Free download pdf