Looking for New Results

Your service will be polling for new results, so it will need to know what the last result fetched was. This is a perfect job for another SharedPreferences entry.

Update QueryPreferences to store the ID of the most recently fetched photo.

Listing 28.6  Adding recent ID preference constant (QueryPreferences.java)

public class QueryPreferences {
    private static final String PREF_SEARCH_QUERY = "searchQuery";
    private static final String PREF_LAST_RESULT_ID = "lastResultId";

    public static String getStoredQuery(Context context) {
        ...
    }

    public static void setStoredQuery(Context context, String query) {
        ...
    }

    public static String getLastResultId(Context context) {
        return PreferenceManager.getDefaultSharedPreferences(context)
                .getString(PREF_LAST_RESULT_ID, null);
    }

    public static void setLastResultId(Context context, String lastResultId) {
        PreferenceManager.getDefaultSharedPreferences(context)
                .edit()
                .putString(PREF_LAST_RESULT_ID, lastResultId)
                .apply();
    }
}

The next step is to fill out your service. Here is what you need to do:

  1. Pull out the current query and the last result ID from the default SharedPreferences.

  2. Fetch the latest result set with FlickrFetchr.

  3. If there are results, grab the first one.

  4. Check to see whether it is different from the last result ID.

  5. Store the first result back in SharedPreferences.

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<GalleryItem> 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
34.227.112.145